test_base.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. from collections import defaultdict
  2. from datetime import datetime
  3. from io import StringIO
  4. import math
  5. import operator
  6. import re
  7. import numpy as np
  8. import pytest
  9. from pandas.compat import IS64
  10. from pandas.errors import InvalidIndexError
  11. from pandas.util._test_decorators import async_mark
  12. from pandas.core.dtypes.common import (
  13. is_any_real_numeric_dtype,
  14. is_numeric_dtype,
  15. is_object_dtype,
  16. )
  17. import pandas as pd
  18. from pandas import (
  19. CategoricalIndex,
  20. DataFrame,
  21. DatetimeIndex,
  22. IntervalIndex,
  23. PeriodIndex,
  24. RangeIndex,
  25. Series,
  26. TimedeltaIndex,
  27. date_range,
  28. period_range,
  29. )
  30. import pandas._testing as tm
  31. from pandas.core.indexes.api import (
  32. Index,
  33. MultiIndex,
  34. _get_combined_index,
  35. ensure_index,
  36. ensure_index_from_sequences,
  37. )
  38. from pandas.tests.indexes.common import Base
  39. class TestIndex(Base):
  40. _index_cls = Index
  41. @pytest.fixture
  42. def simple_index(self) -> Index:
  43. return self._index_cls(list("abcde"))
  44. def test_can_hold_identifiers(self, simple_index):
  45. index = simple_index
  46. key = index[0]
  47. assert index._can_hold_identifiers_and_holds_name(key) is True
  48. @pytest.mark.parametrize("index", ["datetime"], indirect=True)
  49. def test_new_axis(self, index):
  50. # TODO: a bunch of scattered tests check this deprecation is enforced.
  51. # de-duplicate/centralize them.
  52. with pytest.raises(ValueError, match="Multi-dimensional indexing"):
  53. # GH#30588 multi-dimensional indexing deprecated
  54. index[None, :]
  55. def test_constructor_regular(self, index):
  56. tm.assert_contains_all(index, index)
  57. @pytest.mark.parametrize("index", ["string"], indirect=True)
  58. def test_constructor_casting(self, index):
  59. # casting
  60. arr = np.array(index)
  61. new_index = Index(arr)
  62. tm.assert_contains_all(arr, new_index)
  63. tm.assert_index_equal(index, new_index)
  64. @pytest.mark.parametrize("index", ["string"], indirect=True)
  65. def test_constructor_copy(self, index):
  66. arr = np.array(index)
  67. new_index = Index(arr, copy=True, name="name")
  68. assert isinstance(new_index, Index)
  69. assert new_index.name == "name"
  70. tm.assert_numpy_array_equal(arr, new_index.values)
  71. arr[0] = "SOMEBIGLONGSTRING"
  72. assert new_index[0] != "SOMEBIGLONGSTRING"
  73. @pytest.mark.parametrize("cast_as_obj", [True, False])
  74. @pytest.mark.parametrize(
  75. "index",
  76. [
  77. date_range(
  78. "2015-01-01 10:00",
  79. freq="D",
  80. periods=3,
  81. tz="US/Eastern",
  82. name="Green Eggs & Ham",
  83. ), # DTI with tz
  84. date_range("2015-01-01 10:00", freq="D", periods=3), # DTI no tz
  85. pd.timedelta_range("1 days", freq="D", periods=3), # td
  86. period_range("2015-01-01", freq="D", periods=3), # period
  87. ],
  88. )
  89. def test_constructor_from_index_dtlike(self, cast_as_obj, index):
  90. if cast_as_obj:
  91. result = Index(index.astype(object))
  92. else:
  93. result = Index(index)
  94. tm.assert_index_equal(result, index)
  95. if isinstance(index, DatetimeIndex):
  96. assert result.tz == index.tz
  97. if cast_as_obj:
  98. # GH#23524 check that Index(dti, dtype=object) does not
  99. # incorrectly raise ValueError, and that nanoseconds are not
  100. # dropped
  101. index += pd.Timedelta(nanoseconds=50)
  102. result = Index(index, dtype=object)
  103. assert result.dtype == np.object_
  104. assert list(result) == list(index)
  105. @pytest.mark.parametrize(
  106. "index,has_tz",
  107. [
  108. (
  109. date_range("2015-01-01 10:00", freq="D", periods=3, tz="US/Eastern"),
  110. True,
  111. ), # datetimetz
  112. (pd.timedelta_range("1 days", freq="D", periods=3), False), # td
  113. (period_range("2015-01-01", freq="D", periods=3), False), # period
  114. ],
  115. )
  116. def test_constructor_from_series_dtlike(self, index, has_tz):
  117. result = Index(Series(index))
  118. tm.assert_index_equal(result, index)
  119. if has_tz:
  120. assert result.tz == index.tz
  121. def test_constructor_from_series_freq(self):
  122. # GH 6273
  123. # create from a series, passing a freq
  124. dts = ["1-1-1990", "2-1-1990", "3-1-1990", "4-1-1990", "5-1-1990"]
  125. expected = DatetimeIndex(dts, freq="MS")
  126. s = Series(pd.to_datetime(dts))
  127. result = DatetimeIndex(s, freq="MS")
  128. tm.assert_index_equal(result, expected)
  129. def test_constructor_from_frame_series_freq(self):
  130. # GH 6273
  131. # create from a series, passing a freq
  132. dts = ["1-1-1990", "2-1-1990", "3-1-1990", "4-1-1990", "5-1-1990"]
  133. expected = DatetimeIndex(dts, freq="MS")
  134. df = DataFrame(np.random.rand(5, 3))
  135. df["date"] = dts
  136. result = DatetimeIndex(df["date"], freq="MS")
  137. assert df["date"].dtype == object
  138. expected.name = "date"
  139. tm.assert_index_equal(result, expected)
  140. expected = Series(dts, name="date")
  141. tm.assert_series_equal(df["date"], expected)
  142. # GH 6274
  143. # infer freq of same
  144. freq = pd.infer_freq(df["date"])
  145. assert freq == "MS"
  146. def test_constructor_int_dtype_nan(self):
  147. # see gh-15187
  148. data = [np.nan]
  149. expected = Index(data, dtype=np.float64)
  150. result = Index(data, dtype="float")
  151. tm.assert_index_equal(result, expected)
  152. @pytest.mark.parametrize(
  153. "klass,dtype,na_val",
  154. [
  155. (Index, np.float64, np.nan),
  156. (DatetimeIndex, "datetime64[ns]", pd.NaT),
  157. ],
  158. )
  159. def test_index_ctor_infer_nan_nat(self, klass, dtype, na_val):
  160. # GH 13467
  161. na_list = [na_val, na_val]
  162. expected = klass(na_list)
  163. assert expected.dtype == dtype
  164. result = Index(na_list)
  165. tm.assert_index_equal(result, expected)
  166. result = Index(np.array(na_list))
  167. tm.assert_index_equal(result, expected)
  168. @pytest.mark.parametrize(
  169. "vals,dtype",
  170. [
  171. ([1, 2, 3, 4, 5], "int"),
  172. ([1.1, np.nan, 2.2, 3.0], "float"),
  173. (["A", "B", "C", np.nan], "obj"),
  174. ],
  175. )
  176. def test_constructor_simple_new(self, vals, dtype):
  177. index = Index(vals, name=dtype)
  178. result = index._simple_new(index.values, dtype)
  179. tm.assert_index_equal(result, index)
  180. @pytest.mark.parametrize("attr", ["values", "asi8"])
  181. @pytest.mark.parametrize("klass", [Index, DatetimeIndex])
  182. def test_constructor_dtypes_datetime(self, tz_naive_fixture, attr, klass):
  183. # Test constructing with a datetimetz dtype
  184. # .values produces numpy datetimes, so these are considered naive
  185. # .asi8 produces integers, so these are considered epoch timestamps
  186. # ^the above will be true in a later version. Right now we `.view`
  187. # the i8 values as NS_DTYPE, effectively treating them as wall times.
  188. index = date_range("2011-01-01", periods=5)
  189. arg = getattr(index, attr)
  190. index = index.tz_localize(tz_naive_fixture)
  191. dtype = index.dtype
  192. # As of 2.0 astype raises on dt64.astype(dt64tz)
  193. err = tz_naive_fixture is not None
  194. msg = "Cannot use .astype to convert from timezone-naive dtype to"
  195. if attr == "asi8":
  196. result = DatetimeIndex(arg).tz_localize(tz_naive_fixture)
  197. tm.assert_index_equal(result, index)
  198. elif klass is Index:
  199. with pytest.raises(TypeError, match="unexpected keyword"):
  200. klass(arg, tz=tz_naive_fixture)
  201. else:
  202. result = klass(arg, tz=tz_naive_fixture)
  203. tm.assert_index_equal(result, index)
  204. if attr == "asi8":
  205. if err:
  206. with pytest.raises(TypeError, match=msg):
  207. DatetimeIndex(arg).astype(dtype)
  208. else:
  209. result = DatetimeIndex(arg).astype(dtype)
  210. tm.assert_index_equal(result, index)
  211. else:
  212. result = klass(arg, dtype=dtype)
  213. tm.assert_index_equal(result, index)
  214. if attr == "asi8":
  215. result = DatetimeIndex(list(arg)).tz_localize(tz_naive_fixture)
  216. tm.assert_index_equal(result, index)
  217. elif klass is Index:
  218. with pytest.raises(TypeError, match="unexpected keyword"):
  219. klass(arg, tz=tz_naive_fixture)
  220. else:
  221. result = klass(list(arg), tz=tz_naive_fixture)
  222. tm.assert_index_equal(result, index)
  223. if attr == "asi8":
  224. if err:
  225. with pytest.raises(TypeError, match=msg):
  226. DatetimeIndex(list(arg)).astype(dtype)
  227. else:
  228. result = DatetimeIndex(list(arg)).astype(dtype)
  229. tm.assert_index_equal(result, index)
  230. else:
  231. result = klass(list(arg), dtype=dtype)
  232. tm.assert_index_equal(result, index)
  233. @pytest.mark.parametrize("attr", ["values", "asi8"])
  234. @pytest.mark.parametrize("klass", [Index, TimedeltaIndex])
  235. def test_constructor_dtypes_timedelta(self, attr, klass):
  236. index = pd.timedelta_range("1 days", periods=5)
  237. index = index._with_freq(None) # won't be preserved by constructors
  238. dtype = index.dtype
  239. values = getattr(index, attr)
  240. result = klass(values, dtype=dtype)
  241. tm.assert_index_equal(result, index)
  242. result = klass(list(values), dtype=dtype)
  243. tm.assert_index_equal(result, index)
  244. @pytest.mark.parametrize("value", [[], iter([]), (_ for _ in [])])
  245. @pytest.mark.parametrize(
  246. "klass",
  247. [
  248. Index,
  249. CategoricalIndex,
  250. DatetimeIndex,
  251. TimedeltaIndex,
  252. ],
  253. )
  254. def test_constructor_empty(self, value, klass):
  255. empty = klass(value)
  256. assert isinstance(empty, klass)
  257. assert not len(empty)
  258. @pytest.mark.parametrize(
  259. "empty,klass",
  260. [
  261. (PeriodIndex([], freq="B"), PeriodIndex),
  262. (PeriodIndex(iter([]), freq="B"), PeriodIndex),
  263. (PeriodIndex((_ for _ in []), freq="B"), PeriodIndex),
  264. (RangeIndex(step=1), RangeIndex),
  265. (MultiIndex(levels=[[1, 2], ["blue", "red"]], codes=[[], []]), MultiIndex),
  266. ],
  267. )
  268. def test_constructor_empty_special(self, empty, klass):
  269. assert isinstance(empty, klass)
  270. assert not len(empty)
  271. @pytest.mark.parametrize(
  272. "index",
  273. [
  274. "datetime",
  275. "float64",
  276. "float32",
  277. "int64",
  278. "int32",
  279. "period",
  280. "range",
  281. "repeats",
  282. "timedelta",
  283. "tuples",
  284. "uint64",
  285. "uint32",
  286. ],
  287. indirect=True,
  288. )
  289. def test_view_with_args(self, index):
  290. index.view("i8")
  291. @pytest.mark.parametrize(
  292. "index",
  293. [
  294. "string",
  295. pytest.param("categorical", marks=pytest.mark.xfail(reason="gh-25464")),
  296. "bool-object",
  297. "bool-dtype",
  298. "empty",
  299. ],
  300. indirect=True,
  301. )
  302. def test_view_with_args_object_array_raises(self, index):
  303. if index.dtype == bool:
  304. msg = "When changing to a larger dtype"
  305. with pytest.raises(ValueError, match=msg):
  306. index.view("i8")
  307. else:
  308. msg = "Cannot change data-type for object array"
  309. with pytest.raises(TypeError, match=msg):
  310. index.view("i8")
  311. @pytest.mark.parametrize(
  312. "index",
  313. ["int64", "int32", "range"],
  314. indirect=True,
  315. )
  316. def test_astype(self, index):
  317. casted = index.astype("i8")
  318. # it works!
  319. casted.get_loc(5)
  320. # pass on name
  321. index.name = "foobar"
  322. casted = index.astype("i8")
  323. assert casted.name == "foobar"
  324. def test_equals_object(self):
  325. # same
  326. assert Index(["a", "b", "c"]).equals(Index(["a", "b", "c"]))
  327. @pytest.mark.parametrize(
  328. "comp", [Index(["a", "b"]), Index(["a", "b", "d"]), ["a", "b", "c"]]
  329. )
  330. def test_not_equals_object(self, comp):
  331. assert not Index(["a", "b", "c"]).equals(comp)
  332. def test_identical(self):
  333. # index
  334. i1 = Index(["a", "b", "c"])
  335. i2 = Index(["a", "b", "c"])
  336. assert i1.identical(i2)
  337. i1 = i1.rename("foo")
  338. assert i1.equals(i2)
  339. assert not i1.identical(i2)
  340. i2 = i2.rename("foo")
  341. assert i1.identical(i2)
  342. i3 = Index([("a", "a"), ("a", "b"), ("b", "a")])
  343. i4 = Index([("a", "a"), ("a", "b"), ("b", "a")], tupleize_cols=False)
  344. assert not i3.identical(i4)
  345. def test_is_(self):
  346. ind = Index(range(10))
  347. assert ind.is_(ind)
  348. assert ind.is_(ind.view().view().view().view())
  349. assert not ind.is_(Index(range(10)))
  350. assert not ind.is_(ind.copy())
  351. assert not ind.is_(ind.copy(deep=False))
  352. assert not ind.is_(ind[:])
  353. assert not ind.is_(np.array(range(10)))
  354. # quasi-implementation dependent
  355. assert ind.is_(ind.view())
  356. ind2 = ind.view()
  357. ind2.name = "bob"
  358. assert ind.is_(ind2)
  359. assert ind2.is_(ind)
  360. # doesn't matter if Indices are *actually* views of underlying data,
  361. assert not ind.is_(Index(ind.values))
  362. arr = np.array(range(1, 11))
  363. ind1 = Index(arr, copy=False)
  364. ind2 = Index(arr, copy=False)
  365. assert not ind1.is_(ind2)
  366. def test_asof_numeric_vs_bool_raises(self):
  367. left = Index([1, 2, 3])
  368. right = Index([True, False], dtype=object)
  369. msg = "Cannot compare dtypes int64 and bool"
  370. with pytest.raises(TypeError, match=msg):
  371. left.asof(right[0])
  372. # TODO: should right.asof(left[0]) also raise?
  373. with pytest.raises(InvalidIndexError, match=re.escape(str(right))):
  374. left.asof(right)
  375. with pytest.raises(InvalidIndexError, match=re.escape(str(left))):
  376. right.asof(left)
  377. @pytest.mark.parametrize("index", ["string"], indirect=True)
  378. def test_booleanindex(self, index):
  379. bool_index = np.ones(len(index), dtype=bool)
  380. bool_index[5:30:2] = False
  381. sub_index = index[bool_index]
  382. for i, val in enumerate(sub_index):
  383. assert sub_index.get_loc(val) == i
  384. sub_index = index[list(bool_index)]
  385. for i, val in enumerate(sub_index):
  386. assert sub_index.get_loc(val) == i
  387. def test_fancy(self, simple_index):
  388. index = simple_index
  389. sl = index[[1, 2, 3]]
  390. for i in sl:
  391. assert i == sl[sl.get_loc(i)]
  392. @pytest.mark.parametrize(
  393. "index",
  394. ["string", "int64", "int32", "uint64", "uint32", "float64", "float32"],
  395. indirect=True,
  396. )
  397. @pytest.mark.parametrize("dtype", [np.int_, np.bool_])
  398. def test_empty_fancy(self, index, dtype):
  399. empty_arr = np.array([], dtype=dtype)
  400. empty_index = type(index)([], dtype=index.dtype)
  401. assert index[[]].identical(empty_index)
  402. assert index[empty_arr].identical(empty_index)
  403. @pytest.mark.parametrize(
  404. "index",
  405. ["string", "int64", "int32", "uint64", "uint32", "float64", "float32"],
  406. indirect=True,
  407. )
  408. def test_empty_fancy_raises(self, index):
  409. # DatetimeIndex is excluded, because it overrides getitem and should
  410. # be tested separately.
  411. empty_farr = np.array([], dtype=np.float_)
  412. empty_index = type(index)([], dtype=index.dtype)
  413. assert index[[]].identical(empty_index)
  414. # np.ndarray only accepts ndarray of int & bool dtypes, so should Index
  415. msg = r"arrays used as indices must be of integer \(or boolean\) type"
  416. with pytest.raises(IndexError, match=msg):
  417. index[empty_farr]
  418. def test_union_dt_as_obj(self, simple_index):
  419. # TODO: Replace with fixturesult
  420. index = simple_index
  421. date_index = date_range("2019-01-01", periods=10)
  422. first_cat = index.union(date_index)
  423. second_cat = index.union(index)
  424. appended = np.append(index, date_index.astype("O"))
  425. assert tm.equalContents(first_cat, appended)
  426. assert tm.equalContents(second_cat, index)
  427. tm.assert_contains_all(index, first_cat)
  428. tm.assert_contains_all(index, second_cat)
  429. tm.assert_contains_all(date_index, first_cat)
  430. def test_map_with_tuples(self):
  431. # GH 12766
  432. # Test that returning a single tuple from an Index
  433. # returns an Index.
  434. index = tm.makeIntIndex(3)
  435. result = tm.makeIntIndex(3).map(lambda x: (x,))
  436. expected = Index([(i,) for i in index])
  437. tm.assert_index_equal(result, expected)
  438. # Test that returning a tuple from a map of a single index
  439. # returns a MultiIndex object.
  440. result = index.map(lambda x: (x, x == 1))
  441. expected = MultiIndex.from_tuples([(i, i == 1) for i in index])
  442. tm.assert_index_equal(result, expected)
  443. def test_map_with_tuples_mi(self):
  444. # Test that returning a single object from a MultiIndex
  445. # returns an Index.
  446. first_level = ["foo", "bar", "baz"]
  447. multi_index = MultiIndex.from_tuples(zip(first_level, [1, 2, 3]))
  448. reduced_index = multi_index.map(lambda x: x[0])
  449. tm.assert_index_equal(reduced_index, Index(first_level))
  450. @pytest.mark.parametrize(
  451. "attr", ["makeDateIndex", "makePeriodIndex", "makeTimedeltaIndex"]
  452. )
  453. def test_map_tseries_indices_return_index(self, attr):
  454. index = getattr(tm, attr)(10)
  455. expected = Index([1] * 10)
  456. result = index.map(lambda x: 1)
  457. tm.assert_index_equal(expected, result)
  458. def test_map_tseries_indices_accsr_return_index(self):
  459. date_index = tm.makeDateIndex(24, freq="h", name="hourly")
  460. expected = Index(range(24), dtype="int32", name="hourly")
  461. tm.assert_index_equal(expected, date_index.map(lambda x: x.hour), exact=True)
  462. @pytest.mark.parametrize(
  463. "mapper",
  464. [
  465. lambda values, index: {i: e for e, i in zip(values, index)},
  466. lambda values, index: Series(values, index),
  467. ],
  468. )
  469. def test_map_dictlike_simple(self, mapper):
  470. # GH 12756
  471. expected = Index(["foo", "bar", "baz"])
  472. index = tm.makeIntIndex(3)
  473. result = index.map(mapper(expected.values, index))
  474. tm.assert_index_equal(result, expected)
  475. @pytest.mark.parametrize(
  476. "mapper",
  477. [
  478. lambda values, index: {i: e for e, i in zip(values, index)},
  479. lambda values, index: Series(values, index),
  480. ],
  481. )
  482. def test_map_dictlike(self, index, mapper, request):
  483. # GH 12756
  484. if isinstance(index, CategoricalIndex):
  485. # Tested in test_categorical
  486. return
  487. elif not index.is_unique:
  488. # Cannot map duplicated index
  489. return
  490. rng = np.arange(len(index), 0, -1, dtype=np.int64)
  491. if index.empty:
  492. # to match proper result coercion for uints
  493. expected = Index([])
  494. elif is_numeric_dtype(index.dtype):
  495. expected = index._constructor(rng, dtype=index.dtype)
  496. elif type(index) is Index and index.dtype != object:
  497. # i.e. EA-backed, for now just Nullable
  498. expected = Index(rng, dtype=index.dtype)
  499. else:
  500. expected = Index(rng)
  501. result = index.map(mapper(expected, index))
  502. tm.assert_index_equal(result, expected)
  503. @pytest.mark.parametrize(
  504. "mapper",
  505. [Series(["foo", 2.0, "baz"], index=[0, 2, -1]), {0: "foo", 2: 2.0, -1: "baz"}],
  506. )
  507. def test_map_with_non_function_missing_values(self, mapper):
  508. # GH 12756
  509. expected = Index([2.0, np.nan, "foo"])
  510. result = Index([2, 1, 0]).map(mapper)
  511. tm.assert_index_equal(expected, result)
  512. def test_map_na_exclusion(self):
  513. index = Index([1.5, np.nan, 3, np.nan, 5])
  514. result = index.map(lambda x: x * 2, na_action="ignore")
  515. expected = index * 2
  516. tm.assert_index_equal(result, expected)
  517. def test_map_defaultdict(self):
  518. index = Index([1, 2, 3])
  519. default_dict = defaultdict(lambda: "blank")
  520. default_dict[1] = "stuff"
  521. result = index.map(default_dict)
  522. expected = Index(["stuff", "blank", "blank"])
  523. tm.assert_index_equal(result, expected)
  524. @pytest.mark.parametrize("name,expected", [("foo", "foo"), ("bar", None)])
  525. def test_append_empty_preserve_name(self, name, expected):
  526. left = Index([], name="foo")
  527. right = Index([1, 2, 3], name=name)
  528. result = left.append(right)
  529. assert result.name == expected
  530. @pytest.mark.parametrize(
  531. "index, expected",
  532. [
  533. ("string", False),
  534. ("bool-object", False),
  535. ("bool-dtype", False),
  536. ("categorical", False),
  537. ("int64", True),
  538. ("int32", True),
  539. ("uint64", True),
  540. ("uint32", True),
  541. ("datetime", False),
  542. ("float64", True),
  543. ("float32", True),
  544. ],
  545. indirect=["index"],
  546. )
  547. def test_is_numeric(self, index, expected):
  548. assert is_any_real_numeric_dtype(index) is expected
  549. @pytest.mark.parametrize(
  550. "index, expected",
  551. [
  552. ("string", True),
  553. ("bool-object", True),
  554. ("bool-dtype", False),
  555. ("categorical", False),
  556. ("int64", False),
  557. ("int32", False),
  558. ("uint64", False),
  559. ("uint32", False),
  560. ("datetime", False),
  561. ("float64", False),
  562. ("float32", False),
  563. ],
  564. indirect=["index"],
  565. )
  566. def test_is_object(self, index, expected):
  567. assert is_object_dtype(index) is expected
  568. def test_summary(self, index):
  569. index._summary()
  570. def test_format_bug(self):
  571. # GH 14626
  572. # windows has different precision on datetime.datetime.now (it doesn't
  573. # include us since the default for Timestamp shows these but Index
  574. # formatting does not we are skipping)
  575. now = datetime.now()
  576. if not str(now).endswith("000"):
  577. index = Index([now])
  578. formatted = index.format()
  579. expected = [str(index[0])]
  580. assert formatted == expected
  581. Index([]).format()
  582. @pytest.mark.parametrize("vals", [[1, 2.0 + 3.0j, 4.0], ["a", "b", "c"]])
  583. def test_format_missing(self, vals, nulls_fixture):
  584. # 2845
  585. vals = list(vals) # Copy for each iteration
  586. vals.append(nulls_fixture)
  587. index = Index(vals, dtype=object)
  588. # TODO: case with complex dtype?
  589. formatted = index.format()
  590. null_repr = "NaN" if isinstance(nulls_fixture, float) else str(nulls_fixture)
  591. expected = [str(index[0]), str(index[1]), str(index[2]), null_repr]
  592. assert formatted == expected
  593. assert index[3] is nulls_fixture
  594. @pytest.mark.parametrize("op", ["any", "all"])
  595. def test_logical_compat(self, op, simple_index):
  596. index = simple_index
  597. assert getattr(index, op)() == getattr(index.values, op)()
  598. @pytest.mark.parametrize(
  599. "index", ["string", "int64", "int32", "float64", "float32"], indirect=True
  600. )
  601. def test_drop_by_str_label(self, index):
  602. n = len(index)
  603. drop = index[list(range(5, 10))]
  604. dropped = index.drop(drop)
  605. expected = index[list(range(5)) + list(range(10, n))]
  606. tm.assert_index_equal(dropped, expected)
  607. dropped = index.drop(index[0])
  608. expected = index[1:]
  609. tm.assert_index_equal(dropped, expected)
  610. @pytest.mark.parametrize(
  611. "index", ["string", "int64", "int32", "float64", "float32"], indirect=True
  612. )
  613. @pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]])
  614. def test_drop_by_str_label_raises_missing_keys(self, index, keys):
  615. with pytest.raises(KeyError, match=""):
  616. index.drop(keys)
  617. @pytest.mark.parametrize(
  618. "index", ["string", "int64", "int32", "float64", "float32"], indirect=True
  619. )
  620. def test_drop_by_str_label_errors_ignore(self, index):
  621. n = len(index)
  622. drop = index[list(range(5, 10))]
  623. mixed = drop.tolist() + ["foo"]
  624. dropped = index.drop(mixed, errors="ignore")
  625. expected = index[list(range(5)) + list(range(10, n))]
  626. tm.assert_index_equal(dropped, expected)
  627. dropped = index.drop(["foo", "bar"], errors="ignore")
  628. expected = index[list(range(n))]
  629. tm.assert_index_equal(dropped, expected)
  630. def test_drop_by_numeric_label_loc(self):
  631. # TODO: Parametrize numeric and str tests after self.strIndex fixture
  632. index = Index([1, 2, 3])
  633. dropped = index.drop(1)
  634. expected = Index([2, 3])
  635. tm.assert_index_equal(dropped, expected)
  636. def test_drop_by_numeric_label_raises_missing_keys(self):
  637. index = Index([1, 2, 3])
  638. with pytest.raises(KeyError, match=""):
  639. index.drop([3, 4])
  640. @pytest.mark.parametrize(
  641. "key,expected", [(4, Index([1, 2, 3])), ([3, 4, 5], Index([1, 2]))]
  642. )
  643. def test_drop_by_numeric_label_errors_ignore(self, key, expected):
  644. index = Index([1, 2, 3])
  645. dropped = index.drop(key, errors="ignore")
  646. tm.assert_index_equal(dropped, expected)
  647. @pytest.mark.parametrize(
  648. "values",
  649. [["a", "b", ("c", "d")], ["a", ("c", "d"), "b"], [("c", "d"), "a", "b"]],
  650. )
  651. @pytest.mark.parametrize("to_drop", [[("c", "d"), "a"], ["a", ("c", "d")]])
  652. def test_drop_tuple(self, values, to_drop):
  653. # GH 18304
  654. index = Index(values)
  655. expected = Index(["b"])
  656. result = index.drop(to_drop)
  657. tm.assert_index_equal(result, expected)
  658. removed = index.drop(to_drop[0])
  659. for drop_me in to_drop[1], [to_drop[1]]:
  660. result = removed.drop(drop_me)
  661. tm.assert_index_equal(result, expected)
  662. removed = index.drop(to_drop[1])
  663. msg = rf"\"\[{re.escape(to_drop[1].__repr__())}\] not found in axis\""
  664. for drop_me in to_drop[1], [to_drop[1]]:
  665. with pytest.raises(KeyError, match=msg):
  666. removed.drop(drop_me)
  667. def test_drop_with_duplicates_in_index(self, index):
  668. # GH38051
  669. if len(index) == 0 or isinstance(index, MultiIndex):
  670. return
  671. if isinstance(index, IntervalIndex) and not IS64:
  672. pytest.skip("Cannot test IntervalIndex with int64 dtype on 32 bit platform")
  673. index = index.unique().repeat(2)
  674. expected = index[2:]
  675. result = index.drop(index[0])
  676. tm.assert_index_equal(result, expected)
  677. @pytest.mark.parametrize(
  678. "attr",
  679. [
  680. "is_monotonic_increasing",
  681. "is_monotonic_decreasing",
  682. "_is_strictly_monotonic_increasing",
  683. "_is_strictly_monotonic_decreasing",
  684. ],
  685. )
  686. def test_is_monotonic_incomparable(self, attr):
  687. index = Index([5, datetime.now(), 7])
  688. assert not getattr(index, attr)
  689. @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}])
  690. @pytest.mark.parametrize(
  691. "index,expected",
  692. [
  693. (Index(["qux", "baz", "foo", "bar"]), np.array([False, False, True, True])),
  694. (Index([]), np.array([], dtype=bool)), # empty
  695. ],
  696. )
  697. def test_isin(self, values, index, expected):
  698. result = index.isin(values)
  699. tm.assert_numpy_array_equal(result, expected)
  700. def test_isin_nan_common_object(self, nulls_fixture, nulls_fixture2):
  701. # Test cartesian product of null fixtures and ensure that we don't
  702. # mangle the various types (save a corner case with PyPy)
  703. # all nans are the same
  704. if (
  705. isinstance(nulls_fixture, float)
  706. and isinstance(nulls_fixture2, float)
  707. and math.isnan(nulls_fixture)
  708. and math.isnan(nulls_fixture2)
  709. ):
  710. tm.assert_numpy_array_equal(
  711. Index(["a", nulls_fixture]).isin([nulls_fixture2]),
  712. np.array([False, True]),
  713. )
  714. elif nulls_fixture is nulls_fixture2: # should preserve NA type
  715. tm.assert_numpy_array_equal(
  716. Index(["a", nulls_fixture]).isin([nulls_fixture2]),
  717. np.array([False, True]),
  718. )
  719. else:
  720. tm.assert_numpy_array_equal(
  721. Index(["a", nulls_fixture]).isin([nulls_fixture2]),
  722. np.array([False, False]),
  723. )
  724. def test_isin_nan_common_float64(self, nulls_fixture, float_numpy_dtype):
  725. dtype = float_numpy_dtype
  726. if nulls_fixture is pd.NaT or nulls_fixture is pd.NA:
  727. # Check 1) that we cannot construct a float64 Index with this value
  728. # and 2) that with an NaN we do not have .isin(nulls_fixture)
  729. msg = (
  730. r"float\(\) argument must be a string or a (real )?number, "
  731. f"not {repr(type(nulls_fixture).__name__)}"
  732. )
  733. with pytest.raises(TypeError, match=msg):
  734. Index([1.0, nulls_fixture], dtype=dtype)
  735. idx = Index([1.0, np.nan], dtype=dtype)
  736. assert not idx.isin([nulls_fixture]).any()
  737. return
  738. idx = Index([1.0, nulls_fixture], dtype=dtype)
  739. res = idx.isin([np.nan])
  740. tm.assert_numpy_array_equal(res, np.array([False, True]))
  741. # we cannot compare NaT with NaN
  742. res = idx.isin([pd.NaT])
  743. tm.assert_numpy_array_equal(res, np.array([False, False]))
  744. @pytest.mark.parametrize("level", [0, -1])
  745. @pytest.mark.parametrize(
  746. "index",
  747. [
  748. Index(["qux", "baz", "foo", "bar"]),
  749. Index([1.0, 2.0, 3.0, 4.0], dtype=np.float64),
  750. ],
  751. )
  752. def test_isin_level_kwarg(self, level, index):
  753. values = index.tolist()[-2:] + ["nonexisting"]
  754. expected = np.array([False, False, True, True])
  755. tm.assert_numpy_array_equal(expected, index.isin(values, level=level))
  756. index.name = "foobar"
  757. tm.assert_numpy_array_equal(expected, index.isin(values, level="foobar"))
  758. def test_isin_level_kwarg_bad_level_raises(self, index):
  759. for level in [10, index.nlevels, -(index.nlevels + 1)]:
  760. with pytest.raises(IndexError, match="Too many levels"):
  761. index.isin([], level=level)
  762. @pytest.mark.parametrize("label", [1.0, "foobar", "xyzzy", np.nan])
  763. def test_isin_level_kwarg_bad_label_raises(self, label, index):
  764. if isinstance(index, MultiIndex):
  765. index = index.rename(["foo", "bar"] + index.names[2:])
  766. msg = f"'Level {label} not found'"
  767. else:
  768. index = index.rename("foo")
  769. msg = rf"Requested level \({label}\) does not match index name \(foo\)"
  770. with pytest.raises(KeyError, match=msg):
  771. index.isin([], level=label)
  772. @pytest.mark.parametrize("empty", [[], Series(dtype=object), np.array([])])
  773. def test_isin_empty(self, empty):
  774. # see gh-16991
  775. index = Index(["a", "b"])
  776. expected = np.array([False, False])
  777. result = index.isin(empty)
  778. tm.assert_numpy_array_equal(expected, result)
  779. @pytest.mark.parametrize(
  780. "values",
  781. [
  782. [1, 2, 3, 4],
  783. [1.0, 2.0, 3.0, 4.0],
  784. [True, True, True, True],
  785. ["foo", "bar", "baz", "qux"],
  786. date_range("2018-01-01", freq="D", periods=4),
  787. ],
  788. )
  789. def test_boolean_cmp(self, values):
  790. index = Index(values)
  791. result = index == values
  792. expected = np.array([True, True, True, True], dtype=bool)
  793. tm.assert_numpy_array_equal(result, expected)
  794. @pytest.mark.parametrize("index", ["string"], indirect=True)
  795. @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")])
  796. def test_get_level_values(self, index, name, level):
  797. expected = index.copy()
  798. if name:
  799. expected.name = name
  800. result = expected.get_level_values(level)
  801. tm.assert_index_equal(result, expected)
  802. def test_slice_keep_name(self):
  803. index = Index(["a", "b"], name="asdf")
  804. assert index.name == index[1:].name
  805. @pytest.mark.parametrize(
  806. "index",
  807. [
  808. "string",
  809. "datetime",
  810. "int64",
  811. "int32",
  812. "uint64",
  813. "uint32",
  814. "float64",
  815. "float32",
  816. ],
  817. indirect=True,
  818. )
  819. def test_join_self(self, index, join_type):
  820. joined = index.join(index, how=join_type)
  821. assert index is joined
  822. @pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"])
  823. def test_str_attribute(self, method):
  824. # GH9068
  825. index = Index([" jack", "jill ", " jesse ", "frank"])
  826. expected = Index([getattr(str, method)(x) for x in index.values])
  827. result = getattr(index.str, method)()
  828. tm.assert_index_equal(result, expected)
  829. @pytest.mark.parametrize(
  830. "index",
  831. [
  832. Index(range(5)),
  833. tm.makeDateIndex(10),
  834. MultiIndex.from_tuples([("foo", "1"), ("bar", "3")]),
  835. period_range(start="2000", end="2010", freq="A"),
  836. ],
  837. )
  838. def test_str_attribute_raises(self, index):
  839. with pytest.raises(AttributeError, match="only use .str accessor"):
  840. index.str.repeat(2)
  841. @pytest.mark.parametrize(
  842. "expand,expected",
  843. [
  844. (None, Index([["a", "b", "c"], ["d", "e"], ["f"]])),
  845. (False, Index([["a", "b", "c"], ["d", "e"], ["f"]])),
  846. (
  847. True,
  848. MultiIndex.from_tuples(
  849. [("a", "b", "c"), ("d", "e", np.nan), ("f", np.nan, np.nan)]
  850. ),
  851. ),
  852. ],
  853. )
  854. def test_str_split(self, expand, expected):
  855. index = Index(["a b c", "d e", "f"])
  856. if expand is not None:
  857. result = index.str.split(expand=expand)
  858. else:
  859. result = index.str.split()
  860. tm.assert_index_equal(result, expected)
  861. def test_str_bool_return(self):
  862. # test boolean case, should return np.array instead of boolean Index
  863. index = Index(["a1", "a2", "b1", "b2"])
  864. result = index.str.startswith("a")
  865. expected = np.array([True, True, False, False])
  866. tm.assert_numpy_array_equal(result, expected)
  867. assert isinstance(result, np.ndarray)
  868. def test_str_bool_series_indexing(self):
  869. index = Index(["a1", "a2", "b1", "b2"])
  870. s = Series(range(4), index=index)
  871. result = s[s.index.str.startswith("a")]
  872. expected = Series(range(2), index=["a1", "a2"])
  873. tm.assert_series_equal(result, expected)
  874. @pytest.mark.parametrize(
  875. "index,expected", [(Index(list("abcd")), True), (Index(range(4)), False)]
  876. )
  877. def test_tab_completion(self, index, expected):
  878. # GH 9910
  879. result = "str" in dir(index)
  880. assert result == expected
  881. def test_indexing_doesnt_change_class(self):
  882. index = Index([1, 2, 3, "a", "b", "c"])
  883. assert index[1:3].identical(Index([2, 3], dtype=np.object_))
  884. assert index[[0, 1]].identical(Index([1, 2], dtype=np.object_))
  885. def test_outer_join_sort(self):
  886. left_index = Index(np.random.permutation(15))
  887. right_index = tm.makeDateIndex(10)
  888. with tm.assert_produces_warning(RuntimeWarning):
  889. result = left_index.join(right_index, how="outer")
  890. # right_index in this case because DatetimeIndex has join precedence
  891. # over int64 Index
  892. with tm.assert_produces_warning(RuntimeWarning):
  893. expected = right_index.astype(object).union(left_index.astype(object))
  894. tm.assert_index_equal(result, expected)
  895. def test_take_fill_value(self):
  896. # GH 12631
  897. index = Index(list("ABC"), name="xxx")
  898. result = index.take(np.array([1, 0, -1]))
  899. expected = Index(list("BAC"), name="xxx")
  900. tm.assert_index_equal(result, expected)
  901. # fill_value
  902. result = index.take(np.array([1, 0, -1]), fill_value=True)
  903. expected = Index(["B", "A", np.nan], name="xxx")
  904. tm.assert_index_equal(result, expected)
  905. # allow_fill=False
  906. result = index.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
  907. expected = Index(["B", "A", "C"], name="xxx")
  908. tm.assert_index_equal(result, expected)
  909. def test_take_fill_value_none_raises(self):
  910. index = Index(list("ABC"), name="xxx")
  911. msg = (
  912. "When allow_fill=True and fill_value is not None, "
  913. "all indices must be >= -1"
  914. )
  915. with pytest.raises(ValueError, match=msg):
  916. index.take(np.array([1, 0, -2]), fill_value=True)
  917. with pytest.raises(ValueError, match=msg):
  918. index.take(np.array([1, 0, -5]), fill_value=True)
  919. def test_take_bad_bounds_raises(self):
  920. index = Index(list("ABC"), name="xxx")
  921. with pytest.raises(IndexError, match="out of bounds"):
  922. index.take(np.array([1, -5]))
  923. @pytest.mark.parametrize("name", [None, "foobar"])
  924. @pytest.mark.parametrize(
  925. "labels",
  926. [
  927. [],
  928. np.array([]),
  929. ["A", "B", "C"],
  930. ["C", "B", "A"],
  931. np.array(["A", "B", "C"]),
  932. np.array(["C", "B", "A"]),
  933. # Must preserve name even if dtype changes
  934. date_range("20130101", periods=3).values,
  935. date_range("20130101", periods=3).tolist(),
  936. ],
  937. )
  938. def test_reindex_preserves_name_if_target_is_list_or_ndarray(self, name, labels):
  939. # GH6552
  940. index = Index([0, 1, 2])
  941. index.name = name
  942. assert index.reindex(labels)[0].name == name
  943. @pytest.mark.parametrize("labels", [[], np.array([]), np.array([], dtype=np.int64)])
  944. def test_reindex_preserves_type_if_target_is_empty_list_or_array(self, labels):
  945. # GH7774
  946. index = Index(list("abc"))
  947. assert index.reindex(labels)[0].dtype.type == np.object_
  948. @pytest.mark.parametrize(
  949. "labels,dtype",
  950. [
  951. (DatetimeIndex([]), np.datetime64),
  952. ],
  953. )
  954. def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self, labels, dtype):
  955. # GH7774
  956. index = Index(list("abc"))
  957. assert index.reindex(labels)[0].dtype.type == dtype
  958. def test_reindex_doesnt_preserve_type_if_target_is_empty_index_numeric(
  959. self, any_real_numpy_dtype
  960. ):
  961. # GH7774
  962. dtype = any_real_numpy_dtype
  963. index = Index(list("abc"))
  964. labels = Index([], dtype=dtype)
  965. assert index.reindex(labels)[0].dtype == dtype
  966. def test_reindex_no_type_preserve_target_empty_mi(self):
  967. index = Index(list("abc"))
  968. result = index.reindex(
  969. MultiIndex([Index([], np.int64), Index([], np.float64)], [[], []])
  970. )[0]
  971. assert result.levels[0].dtype.type == np.int64
  972. assert result.levels[1].dtype.type == np.float64
  973. def test_reindex_ignoring_level(self):
  974. # GH#35132
  975. idx = Index([1, 2, 3], name="x")
  976. idx2 = Index([1, 2, 3, 4], name="x")
  977. expected = Index([1, 2, 3, 4], name="x")
  978. result, _ = idx.reindex(idx2, level="x")
  979. tm.assert_index_equal(result, expected)
  980. def test_groupby(self):
  981. index = Index(range(5))
  982. result = index.groupby(np.array([1, 1, 2, 2, 2]))
  983. expected = {1: Index([0, 1]), 2: Index([2, 3, 4])}
  984. tm.assert_dict_equal(result, expected)
  985. @pytest.mark.parametrize(
  986. "mi,expected",
  987. [
  988. (MultiIndex.from_tuples([(1, 2), (4, 5)]), np.array([True, True])),
  989. (MultiIndex.from_tuples([(1, 2), (4, 6)]), np.array([True, False])),
  990. ],
  991. )
  992. def test_equals_op_multiindex(self, mi, expected):
  993. # GH9785
  994. # test comparisons of multiindex
  995. df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])
  996. result = df.index == mi
  997. tm.assert_numpy_array_equal(result, expected)
  998. def test_equals_op_multiindex_identify(self):
  999. df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])
  1000. result = df.index == df.index
  1001. expected = np.array([True, True])
  1002. tm.assert_numpy_array_equal(result, expected)
  1003. @pytest.mark.parametrize(
  1004. "index",
  1005. [
  1006. MultiIndex.from_tuples([(1, 2), (4, 5), (8, 9)]),
  1007. Index(["foo", "bar", "baz"]),
  1008. ],
  1009. )
  1010. def test_equals_op_mismatched_multiindex_raises(self, index):
  1011. df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])
  1012. with pytest.raises(ValueError, match="Lengths must match"):
  1013. df.index == index
  1014. def test_equals_op_index_vs_mi_same_length(self):
  1015. mi = MultiIndex.from_tuples([(1, 2), (4, 5), (8, 9)])
  1016. index = Index(["foo", "bar", "baz"])
  1017. result = mi == index
  1018. expected = np.array([False, False, False])
  1019. tm.assert_numpy_array_equal(result, expected)
  1020. @pytest.mark.parametrize(
  1021. "dt_conv, arg",
  1022. [
  1023. (pd.to_datetime, ["2000-01-01", "2000-01-02"]),
  1024. (pd.to_timedelta, ["01:02:03", "01:02:04"]),
  1025. ],
  1026. )
  1027. def test_dt_conversion_preserves_name(self, dt_conv, arg):
  1028. # GH 10875
  1029. index = Index(arg, name="label")
  1030. assert index.name == dt_conv(index).name
  1031. def test_cached_properties_not_settable(self):
  1032. index = Index([1, 2, 3])
  1033. with pytest.raises(AttributeError, match="Can't set attribute"):
  1034. index.is_unique = False
  1035. @async_mark()
  1036. async def test_tab_complete_warning(self, ip):
  1037. # https://github.com/pandas-dev/pandas/issues/16409
  1038. pytest.importorskip("IPython", minversion="6.0.0")
  1039. from IPython.core.completer import provisionalcompleter
  1040. code = "import pandas as pd; idx = pd.Index([1, 2])"
  1041. await ip.run_code(code)
  1042. # GH 31324 newer jedi version raises Deprecation warning;
  1043. # appears resolved 2021-02-02
  1044. with tm.assert_produces_warning(None):
  1045. with provisionalcompleter("ignore"):
  1046. list(ip.Completer.completions("idx.", 4))
  1047. def test_contains_method_removed(self, index):
  1048. # GH#30103 method removed for all types except IntervalIndex
  1049. if isinstance(index, IntervalIndex):
  1050. index.contains(1)
  1051. else:
  1052. msg = f"'{type(index).__name__}' object has no attribute 'contains'"
  1053. with pytest.raises(AttributeError, match=msg):
  1054. index.contains(1)
  1055. def test_sortlevel(self):
  1056. index = Index([5, 4, 3, 2, 1])
  1057. with pytest.raises(Exception, match="ascending must be a single bool value or"):
  1058. index.sortlevel(ascending="True")
  1059. with pytest.raises(
  1060. Exception, match="ascending must be a list of bool values of length 1"
  1061. ):
  1062. index.sortlevel(ascending=[True, True])
  1063. with pytest.raises(Exception, match="ascending must be a bool value"):
  1064. index.sortlevel(ascending=["True"])
  1065. expected = Index([1, 2, 3, 4, 5])
  1066. result = index.sortlevel(ascending=[True])
  1067. tm.assert_index_equal(result[0], expected)
  1068. expected = Index([1, 2, 3, 4, 5])
  1069. result = index.sortlevel(ascending=True)
  1070. tm.assert_index_equal(result[0], expected)
  1071. expected = Index([5, 4, 3, 2, 1])
  1072. result = index.sortlevel(ascending=False)
  1073. tm.assert_index_equal(result[0], expected)
  1074. class TestMixedIntIndex(Base):
  1075. # Mostly the tests from common.py for which the results differ
  1076. # in py2 and py3 because ints and strings are uncomparable in py3
  1077. # (GH 13514)
  1078. _index_cls = Index
  1079. @pytest.fixture
  1080. def simple_index(self) -> Index:
  1081. return self._index_cls([0, "a", 1, "b", 2, "c"])
  1082. @pytest.fixture(params=[[0, "a", 1, "b", 2, "c"]], ids=["mixedIndex"])
  1083. def index(self, request):
  1084. return Index(request.param)
  1085. def test_argsort(self, simple_index):
  1086. index = simple_index
  1087. with pytest.raises(TypeError, match="'>|<' not supported"):
  1088. index.argsort()
  1089. def test_numpy_argsort(self, simple_index):
  1090. index = simple_index
  1091. with pytest.raises(TypeError, match="'>|<' not supported"):
  1092. np.argsort(index)
  1093. def test_copy_name(self, simple_index):
  1094. # Check that "name" argument passed at initialization is honoured
  1095. # GH12309
  1096. index = simple_index
  1097. first = type(index)(index, copy=True, name="mario")
  1098. second = type(first)(first, copy=False)
  1099. # Even though "copy=False", we want a new object.
  1100. assert first is not second
  1101. tm.assert_index_equal(first, second)
  1102. assert first.name == "mario"
  1103. assert second.name == "mario"
  1104. s1 = Series(2, index=first)
  1105. s2 = Series(3, index=second[:-1])
  1106. s3 = s1 * s2
  1107. assert s3.index.name == "mario"
  1108. def test_copy_name2(self):
  1109. # Check that adding a "name" parameter to the copy is honored
  1110. # GH14302
  1111. index = Index([1, 2], name="MyName")
  1112. index1 = index.copy()
  1113. tm.assert_index_equal(index, index1)
  1114. index2 = index.copy(name="NewName")
  1115. tm.assert_index_equal(index, index2, check_names=False)
  1116. assert index.name == "MyName"
  1117. assert index2.name == "NewName"
  1118. def test_unique_na(self):
  1119. idx = Index([2, np.nan, 2, 1], name="my_index")
  1120. expected = Index([2, np.nan, 1], name="my_index")
  1121. result = idx.unique()
  1122. tm.assert_index_equal(result, expected)
  1123. def test_logical_compat(self, simple_index):
  1124. index = simple_index
  1125. assert index.all() == index.values.all()
  1126. assert index.any() == index.values.any()
  1127. @pytest.mark.parametrize("how", ["any", "all"])
  1128. @pytest.mark.parametrize("dtype", [None, object, "category"])
  1129. @pytest.mark.parametrize(
  1130. "vals,expected",
  1131. [
  1132. ([1, 2, 3], [1, 2, 3]),
  1133. ([1.0, 2.0, 3.0], [1.0, 2.0, 3.0]),
  1134. ([1.0, 2.0, np.nan, 3.0], [1.0, 2.0, 3.0]),
  1135. (["A", "B", "C"], ["A", "B", "C"]),
  1136. (["A", np.nan, "B", "C"], ["A", "B", "C"]),
  1137. ],
  1138. )
  1139. def test_dropna(self, how, dtype, vals, expected):
  1140. # GH 6194
  1141. index = Index(vals, dtype=dtype)
  1142. result = index.dropna(how=how)
  1143. expected = Index(expected, dtype=dtype)
  1144. tm.assert_index_equal(result, expected)
  1145. @pytest.mark.parametrize("how", ["any", "all"])
  1146. @pytest.mark.parametrize(
  1147. "index,expected",
  1148. [
  1149. (
  1150. DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),
  1151. DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),
  1152. ),
  1153. (
  1154. DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03", pd.NaT]),
  1155. DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),
  1156. ),
  1157. (
  1158. TimedeltaIndex(["1 days", "2 days", "3 days"]),
  1159. TimedeltaIndex(["1 days", "2 days", "3 days"]),
  1160. ),
  1161. (
  1162. TimedeltaIndex([pd.NaT, "1 days", "2 days", "3 days", pd.NaT]),
  1163. TimedeltaIndex(["1 days", "2 days", "3 days"]),
  1164. ),
  1165. (
  1166. PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),
  1167. PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),
  1168. ),
  1169. (
  1170. PeriodIndex(["2012-02", "2012-04", "NaT", "2012-05"], freq="M"),
  1171. PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),
  1172. ),
  1173. ],
  1174. )
  1175. def test_dropna_dt_like(self, how, index, expected):
  1176. result = index.dropna(how=how)
  1177. tm.assert_index_equal(result, expected)
  1178. def test_dropna_invalid_how_raises(self):
  1179. msg = "invalid how option: xxx"
  1180. with pytest.raises(ValueError, match=msg):
  1181. Index([1, 2, 3]).dropna(how="xxx")
  1182. @pytest.mark.parametrize(
  1183. "index",
  1184. [
  1185. Index([np.nan]),
  1186. Index([np.nan, 1]),
  1187. Index([1, 2, np.nan]),
  1188. Index(["a", "b", np.nan]),
  1189. pd.to_datetime(["NaT"]),
  1190. pd.to_datetime(["NaT", "2000-01-01"]),
  1191. pd.to_datetime(["2000-01-01", "NaT", "2000-01-02"]),
  1192. pd.to_timedelta(["1 day", "NaT"]),
  1193. ],
  1194. )
  1195. def test_is_monotonic_na(self, index):
  1196. assert index.is_monotonic_increasing is False
  1197. assert index.is_monotonic_decreasing is False
  1198. assert index._is_strictly_monotonic_increasing is False
  1199. assert index._is_strictly_monotonic_decreasing is False
  1200. def test_int_name_format(self, frame_or_series):
  1201. index = Index(["a", "b", "c"], name=0)
  1202. result = frame_or_series(list(range(3)), index=index)
  1203. assert "0" in repr(result)
  1204. def test_str_to_bytes_raises(self):
  1205. # GH 26447
  1206. index = Index([str(x) for x in range(10)])
  1207. msg = "^'str' object cannot be interpreted as an integer$"
  1208. with pytest.raises(TypeError, match=msg):
  1209. bytes(index)
  1210. @pytest.mark.filterwarnings("ignore:elementwise comparison failed:FutureWarning")
  1211. def test_index_with_tuple_bool(self):
  1212. # GH34123
  1213. # TODO: also this op right now produces FutureWarning from numpy
  1214. # https://github.com/numpy/numpy/issues/11521
  1215. idx = Index([("a", "b"), ("b", "c"), ("c", "a")])
  1216. result = idx == ("c", "a")
  1217. expected = np.array([False, False, True])
  1218. tm.assert_numpy_array_equal(result, expected)
  1219. class TestIndexUtils:
  1220. @pytest.mark.parametrize(
  1221. "data, names, expected",
  1222. [
  1223. ([[1, 2, 3]], None, Index([1, 2, 3])),
  1224. ([[1, 2, 3]], ["name"], Index([1, 2, 3], name="name")),
  1225. (
  1226. [["a", "a"], ["c", "d"]],
  1227. None,
  1228. MultiIndex([["a"], ["c", "d"]], [[0, 0], [0, 1]]),
  1229. ),
  1230. (
  1231. [["a", "a"], ["c", "d"]],
  1232. ["L1", "L2"],
  1233. MultiIndex([["a"], ["c", "d"]], [[0, 0], [0, 1]], names=["L1", "L2"]),
  1234. ),
  1235. ],
  1236. )
  1237. def test_ensure_index_from_sequences(self, data, names, expected):
  1238. result = ensure_index_from_sequences(data, names)
  1239. tm.assert_index_equal(result, expected)
  1240. def test_ensure_index_mixed_closed_intervals(self):
  1241. # GH27172
  1242. intervals = [
  1243. pd.Interval(0, 1, closed="left"),
  1244. pd.Interval(1, 2, closed="right"),
  1245. pd.Interval(2, 3, closed="neither"),
  1246. pd.Interval(3, 4, closed="both"),
  1247. ]
  1248. result = ensure_index(intervals)
  1249. expected = Index(intervals, dtype=object)
  1250. tm.assert_index_equal(result, expected)
  1251. def test_ensure_index_uint64(self):
  1252. # with both 0 and a large-uint64, np.array will infer to float64
  1253. # https://github.com/numpy/numpy/issues/19146
  1254. # but a more accurate choice would be uint64
  1255. values = [0, np.iinfo(np.uint64).max]
  1256. result = ensure_index(values)
  1257. assert list(result) == values
  1258. expected = Index(values, dtype="uint64")
  1259. tm.assert_index_equal(result, expected)
  1260. def test_get_combined_index(self):
  1261. result = _get_combined_index([])
  1262. expected = Index([])
  1263. tm.assert_index_equal(result, expected)
  1264. @pytest.mark.parametrize(
  1265. "opname",
  1266. [
  1267. "eq",
  1268. "ne",
  1269. "le",
  1270. "lt",
  1271. "ge",
  1272. "gt",
  1273. "add",
  1274. "radd",
  1275. "sub",
  1276. "rsub",
  1277. "mul",
  1278. "rmul",
  1279. "truediv",
  1280. "rtruediv",
  1281. "floordiv",
  1282. "rfloordiv",
  1283. "pow",
  1284. "rpow",
  1285. "mod",
  1286. "divmod",
  1287. ],
  1288. )
  1289. def test_generated_op_names(opname, index):
  1290. opname = f"__{opname}__"
  1291. method = getattr(index, opname)
  1292. assert method.__name__ == opname
  1293. @pytest.mark.parametrize("index_maker", tm.index_subclass_makers_generator())
  1294. def test_index_subclass_constructor_wrong_kwargs(index_maker):
  1295. # GH #19348
  1296. with pytest.raises(TypeError, match="unexpected keyword argument"):
  1297. index_maker(foo="bar")
  1298. def test_deprecated_fastpath():
  1299. msg = "[Uu]nexpected keyword argument"
  1300. with pytest.raises(TypeError, match=msg):
  1301. Index(np.array(["a", "b"], dtype=object), name="test", fastpath=True)
  1302. with pytest.raises(TypeError, match=msg):
  1303. Index(np.array([1, 2, 3], dtype="int64"), name="test", fastpath=True)
  1304. with pytest.raises(TypeError, match=msg):
  1305. RangeIndex(0, 5, 2, name="test", fastpath=True)
  1306. with pytest.raises(TypeError, match=msg):
  1307. CategoricalIndex(["a", "b", "c"], name="test", fastpath=True)
  1308. def test_shape_of_invalid_index():
  1309. # Pre-2.0, it was possible to create "invalid" index objects backed by
  1310. # a multi-dimensional array (see https://github.com/pandas-dev/pandas/issues/27125
  1311. # about this). However, as long as this is not solved in general,this test ensures
  1312. # that the returned shape is consistent with this underlying array for
  1313. # compat with matplotlib (see https://github.com/pandas-dev/pandas/issues/27775)
  1314. idx = Index([0, 1, 2, 3])
  1315. with pytest.raises(ValueError, match="Multi-dimensional indexing"):
  1316. # GH#30588 multi-dimensional indexing deprecated
  1317. idx[:, None]
  1318. @pytest.mark.parametrize("dtype", [None, np.int64, np.uint64, np.float64])
  1319. def test_validate_1d_input(dtype):
  1320. # GH#27125 check that we do not have >1-dimensional input
  1321. msg = "Index data must be 1-dimensional"
  1322. arr = np.arange(8).reshape(2, 2, 2)
  1323. with pytest.raises(ValueError, match=msg):
  1324. Index(arr, dtype=dtype)
  1325. df = DataFrame(arr.reshape(4, 2))
  1326. with pytest.raises(ValueError, match=msg):
  1327. Index(df, dtype=dtype)
  1328. # GH#13601 trying to assign a multi-dimensional array to an index is not allowed
  1329. ser = Series(0, range(4))
  1330. with pytest.raises(ValueError, match=msg):
  1331. ser.index = np.array([[2, 3]] * 4, dtype=dtype)
  1332. @pytest.mark.parametrize(
  1333. "klass, extra_kwargs",
  1334. [
  1335. [Index, {}],
  1336. *[[lambda x: Index(x, dtype=dtyp), {}] for dtyp in tm.ALL_REAL_NUMPY_DTYPES],
  1337. [DatetimeIndex, {}],
  1338. [TimedeltaIndex, {}],
  1339. [PeriodIndex, {"freq": "Y"}],
  1340. ],
  1341. )
  1342. def test_construct_from_memoryview(klass, extra_kwargs):
  1343. # GH 13120
  1344. result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
  1345. expected = klass(list(range(2000, 2005)), **extra_kwargs)
  1346. tm.assert_index_equal(result, expected, exact=True)
  1347. @pytest.mark.parametrize("op", [operator.lt, operator.gt])
  1348. def test_nan_comparison_same_object(op):
  1349. # GH#47105
  1350. idx = Index([np.nan])
  1351. expected = np.array([False])
  1352. result = op(idx, idx)
  1353. tm.assert_numpy_array_equal(result, expected)
  1354. result = op(idx, idx.copy())
  1355. tm.assert_numpy_array_equal(result, expected)