test_numeric.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import (
  5. Index,
  6. Series,
  7. )
  8. import pandas._testing as tm
  9. class TestFloatNumericIndex:
  10. @pytest.fixture(params=[np.float64, np.float32])
  11. def dtype(self, request):
  12. return request.param
  13. @pytest.fixture
  14. def simple_index(self, dtype):
  15. values = np.arange(5, dtype=dtype)
  16. return Index(values)
  17. @pytest.fixture(
  18. params=[
  19. [1.5, 2, 3, 4, 5],
  20. [0.0, 2.5, 5.0, 7.5, 10.0],
  21. [5, 4, 3, 2, 1.5],
  22. [10.0, 7.5, 5.0, 2.5, 0.0],
  23. ],
  24. ids=["mixed", "float", "mixed_dec", "float_dec"],
  25. )
  26. def index(self, request, dtype):
  27. return Index(request.param, dtype=dtype)
  28. @pytest.fixture
  29. def mixed_index(self, dtype):
  30. return Index([1.5, 2, 3, 4, 5], dtype=dtype)
  31. @pytest.fixture
  32. def float_index(self, dtype):
  33. return Index([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)
  34. def test_repr_roundtrip(self, index):
  35. tm.assert_index_equal(eval(repr(index)), index, exact=True)
  36. def check_coerce(self, a, b, is_float_index=True):
  37. assert a.equals(b)
  38. tm.assert_index_equal(a, b, exact=False)
  39. if is_float_index:
  40. assert isinstance(b, Index)
  41. else:
  42. assert type(b) is Index
  43. def test_constructor_from_list_no_dtype(self):
  44. index = Index([1.5, 2.5, 3.5])
  45. assert index.dtype == np.float64
  46. def test_constructor(self, dtype):
  47. index_cls = Index
  48. # explicit construction
  49. index = index_cls([1, 2, 3, 4, 5], dtype=dtype)
  50. assert isinstance(index, index_cls)
  51. assert index.dtype == dtype
  52. expected = np.array([1, 2, 3, 4, 5], dtype=dtype)
  53. tm.assert_numpy_array_equal(index.values, expected)
  54. index = index_cls(np.array([1, 2, 3, 4, 5]), dtype=dtype)
  55. assert isinstance(index, index_cls)
  56. assert index.dtype == dtype
  57. index = index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
  58. assert isinstance(index, index_cls)
  59. assert index.dtype == dtype
  60. index = index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
  61. assert isinstance(index, index_cls)
  62. assert index.dtype == dtype
  63. index = index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
  64. assert isinstance(index, index_cls)
  65. assert index.dtype == dtype
  66. index = index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
  67. assert isinstance(index, index_cls)
  68. assert index.dtype == dtype
  69. # nan handling
  70. result = index_cls([np.nan, np.nan], dtype=dtype)
  71. assert pd.isna(result.values).all()
  72. result = index_cls(np.array([np.nan]), dtype=dtype)
  73. assert pd.isna(result.values).all()
  74. def test_constructor_invalid(self):
  75. index_cls = Index
  76. cls_name = index_cls.__name__
  77. # invalid
  78. msg = (
  79. rf"{cls_name}\(\.\.\.\) must be called with a collection of "
  80. r"some kind, 0\.0 was passed"
  81. )
  82. with pytest.raises(TypeError, match=msg):
  83. index_cls(0.0)
  84. def test_constructor_coerce(self, mixed_index, float_index):
  85. self.check_coerce(mixed_index, Index([1.5, 2, 3, 4, 5]))
  86. self.check_coerce(float_index, Index(np.arange(5) * 2.5))
  87. result = Index(np.array(np.arange(5) * 2.5, dtype=object))
  88. assert result.dtype == object # as of 2.0 to match Series
  89. self.check_coerce(float_index, result.astype("float64"))
  90. def test_constructor_explicit(self, mixed_index, float_index):
  91. # these don't auto convert
  92. self.check_coerce(
  93. float_index, Index((np.arange(5) * 2.5), dtype=object), is_float_index=False
  94. )
  95. self.check_coerce(
  96. mixed_index, Index([1.5, 2, 3, 4, 5], dtype=object), is_float_index=False
  97. )
  98. def test_type_coercion_fail(self, any_int_numpy_dtype):
  99. # see gh-15832
  100. msg = "Trying to coerce float values to integers"
  101. with pytest.raises(ValueError, match=msg):
  102. Index([1, 2, 3.5], dtype=any_int_numpy_dtype)
  103. def test_equals_numeric(self):
  104. index_cls = Index
  105. idx = index_cls([1.0, 2.0])
  106. assert idx.equals(idx)
  107. assert idx.identical(idx)
  108. idx2 = index_cls([1.0, 2.0])
  109. assert idx.equals(idx2)
  110. idx = index_cls([1.0, np.nan])
  111. assert idx.equals(idx)
  112. assert idx.identical(idx)
  113. idx2 = index_cls([1.0, np.nan])
  114. assert idx.equals(idx2)
  115. @pytest.mark.parametrize(
  116. "other",
  117. (
  118. Index([1, 2], dtype=np.int64),
  119. Index([1.0, 2.0], dtype=object),
  120. Index([1, 2], dtype=object),
  121. ),
  122. )
  123. def test_equals_numeric_other_index_type(self, other):
  124. idx = Index([1.0, 2.0])
  125. assert idx.equals(other)
  126. assert other.equals(idx)
  127. @pytest.mark.parametrize(
  128. "vals",
  129. [
  130. pd.date_range("2016-01-01", periods=3),
  131. pd.timedelta_range("1 Day", periods=3),
  132. ],
  133. )
  134. def test_lookups_datetimelike_values(self, vals, dtype):
  135. # If we have datetime64 or timedelta64 values, make sure they are
  136. # wrapped correctly GH#31163
  137. ser = Series(vals, index=range(3, 6))
  138. ser.index = ser.index.astype(dtype)
  139. expected = vals[1]
  140. result = ser[4.0]
  141. assert isinstance(result, type(expected)) and result == expected
  142. result = ser[4]
  143. assert isinstance(result, type(expected)) and result == expected
  144. result = ser.loc[4.0]
  145. assert isinstance(result, type(expected)) and result == expected
  146. result = ser.loc[4]
  147. assert isinstance(result, type(expected)) and result == expected
  148. result = ser.at[4.0]
  149. assert isinstance(result, type(expected)) and result == expected
  150. # GH#31329 .at[4] should cast to 4.0, matching .loc behavior
  151. result = ser.at[4]
  152. assert isinstance(result, type(expected)) and result == expected
  153. result = ser.iloc[1]
  154. assert isinstance(result, type(expected)) and result == expected
  155. result = ser.iat[1]
  156. assert isinstance(result, type(expected)) and result == expected
  157. def test_doesnt_contain_all_the_things(self):
  158. idx = Index([np.nan])
  159. assert not idx.isin([0]).item()
  160. assert not idx.isin([1]).item()
  161. assert idx.isin([np.nan]).item()
  162. def test_nan_multiple_containment(self):
  163. index_cls = Index
  164. idx = index_cls([1.0, np.nan])
  165. tm.assert_numpy_array_equal(idx.isin([1.0]), np.array([True, False]))
  166. tm.assert_numpy_array_equal(idx.isin([2.0, np.pi]), np.array([False, False]))
  167. tm.assert_numpy_array_equal(idx.isin([np.nan]), np.array([False, True]))
  168. tm.assert_numpy_array_equal(idx.isin([1.0, np.nan]), np.array([True, True]))
  169. idx = index_cls([1.0, 2.0])
  170. tm.assert_numpy_array_equal(idx.isin([np.nan]), np.array([False, False]))
  171. def test_fillna_float64(self):
  172. index_cls = Index
  173. # GH 11343
  174. idx = Index([1.0, np.nan, 3.0], dtype=float, name="x")
  175. # can't downcast
  176. exp = Index([1.0, 0.1, 3.0], name="x")
  177. tm.assert_index_equal(idx.fillna(0.1), exp, exact=True)
  178. # downcast
  179. exp = index_cls([1.0, 2.0, 3.0], name="x")
  180. tm.assert_index_equal(idx.fillna(2), exp)
  181. # object
  182. exp = Index([1.0, "obj", 3.0], name="x")
  183. tm.assert_index_equal(idx.fillna("obj"), exp, exact=True)
  184. def test_logical_compat(self, simple_index):
  185. idx = simple_index
  186. assert idx.all() == idx.values.all()
  187. assert idx.any() == idx.values.any()
  188. assert idx.all() == idx.to_series().all()
  189. assert idx.any() == idx.to_series().any()
  190. class TestNumericInt:
  191. @pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8, np.uint64])
  192. def dtype(self, request):
  193. return request.param
  194. @pytest.fixture
  195. def simple_index(self, dtype):
  196. return Index(range(0, 20, 2), dtype=dtype)
  197. def test_is_monotonic(self):
  198. index_cls = Index
  199. index = index_cls([1, 2, 3, 4])
  200. assert index.is_monotonic_increasing is True
  201. assert index.is_monotonic_increasing is True
  202. assert index._is_strictly_monotonic_increasing is True
  203. assert index.is_monotonic_decreasing is False
  204. assert index._is_strictly_monotonic_decreasing is False
  205. index = index_cls([4, 3, 2, 1])
  206. assert index.is_monotonic_increasing is False
  207. assert index._is_strictly_monotonic_increasing is False
  208. assert index._is_strictly_monotonic_decreasing is True
  209. index = index_cls([1])
  210. assert index.is_monotonic_increasing is True
  211. assert index.is_monotonic_increasing is True
  212. assert index.is_monotonic_decreasing is True
  213. assert index._is_strictly_monotonic_increasing is True
  214. assert index._is_strictly_monotonic_decreasing is True
  215. def test_is_strictly_monotonic(self):
  216. index_cls = Index
  217. index = index_cls([1, 1, 2, 3])
  218. assert index.is_monotonic_increasing is True
  219. assert index._is_strictly_monotonic_increasing is False
  220. index = index_cls([3, 2, 1, 1])
  221. assert index.is_monotonic_decreasing is True
  222. assert index._is_strictly_monotonic_decreasing is False
  223. index = index_cls([1, 1])
  224. assert index.is_monotonic_increasing
  225. assert index.is_monotonic_decreasing
  226. assert not index._is_strictly_monotonic_increasing
  227. assert not index._is_strictly_monotonic_decreasing
  228. def test_logical_compat(self, simple_index):
  229. idx = simple_index
  230. assert idx.all() == idx.values.all()
  231. assert idx.any() == idx.values.any()
  232. def test_identical(self, simple_index, dtype):
  233. index = simple_index
  234. idx = Index(index.copy())
  235. assert idx.identical(index)
  236. same_values_different_type = Index(idx, dtype=object)
  237. assert not idx.identical(same_values_different_type)
  238. idx = index.astype(dtype=object)
  239. idx = idx.rename("foo")
  240. same_values = Index(idx, dtype=object)
  241. assert same_values.identical(idx)
  242. assert not idx.identical(index)
  243. assert Index(same_values, name="foo", dtype=object).identical(idx)
  244. assert not index.astype(dtype=object).identical(index.astype(dtype=dtype))
  245. def test_cant_or_shouldnt_cast(self, dtype):
  246. msg = r"invalid literal for int\(\) with base 10: 'foo'"
  247. # can't
  248. data = ["foo", "bar", "baz"]
  249. with pytest.raises(ValueError, match=msg):
  250. Index(data, dtype=dtype)
  251. def test_view_index(self, simple_index):
  252. index = simple_index
  253. msg = "Passing a type in .*Index.view is deprecated"
  254. with tm.assert_produces_warning(FutureWarning, match=msg):
  255. index.view(Index)
  256. def test_prevent_casting(self, simple_index):
  257. index = simple_index
  258. result = index.astype("O")
  259. assert result.dtype == np.object_
  260. class TestIntNumericIndex:
  261. @pytest.fixture(params=[np.int64, np.int32, np.int16, np.int8])
  262. def dtype(self, request):
  263. return request.param
  264. def test_constructor_from_list_no_dtype(self):
  265. index = Index([1, 2, 3])
  266. assert index.dtype == np.int64
  267. def test_constructor(self, dtype):
  268. index_cls = Index
  269. # scalar raise Exception
  270. msg = (
  271. rf"{index_cls.__name__}\(\.\.\.\) must be called with a collection of some "
  272. "kind, 5 was passed"
  273. )
  274. with pytest.raises(TypeError, match=msg):
  275. index_cls(5)
  276. # copy
  277. # pass list, coerce fine
  278. index = index_cls([-5, 0, 1, 2], dtype=dtype)
  279. arr = index.values.copy()
  280. new_index = index_cls(arr, copy=True)
  281. tm.assert_index_equal(new_index, index, exact=True)
  282. val = int(arr[0]) + 3000
  283. # this should not change index
  284. if dtype != np.int8:
  285. # NEP 50 won't allow assignment that would overflow
  286. arr[0] = val
  287. assert new_index[0] != val
  288. if dtype == np.int64:
  289. # pass list, coerce fine
  290. index = index_cls([-5, 0, 1, 2], dtype=dtype)
  291. expected = Index([-5, 0, 1, 2], dtype=dtype)
  292. tm.assert_index_equal(index, expected)
  293. # from iterable
  294. index = index_cls(iter([-5, 0, 1, 2]), dtype=dtype)
  295. expected = index_cls([-5, 0, 1, 2], dtype=dtype)
  296. tm.assert_index_equal(index, expected, exact=True)
  297. # interpret list-like
  298. expected = index_cls([5, 0], dtype=dtype)
  299. for cls in [Index, index_cls]:
  300. for idx in [
  301. cls([5, 0], dtype=dtype),
  302. cls(np.array([5, 0]), dtype=dtype),
  303. cls(Series([5, 0]), dtype=dtype),
  304. ]:
  305. tm.assert_index_equal(idx, expected)
  306. def test_constructor_corner(self, dtype):
  307. index_cls = Index
  308. arr = np.array([1, 2, 3, 4], dtype=object)
  309. index = index_cls(arr, dtype=dtype)
  310. assert index.values.dtype == index.dtype
  311. if dtype == np.int64:
  312. without_dtype = Index(arr)
  313. # as of 2.0 we do not infer a dtype when we get an object-dtype
  314. # ndarray of numbers, matching Series behavior
  315. assert without_dtype.dtype == object
  316. tm.assert_index_equal(index, without_dtype.astype(np.int64))
  317. # preventing casting
  318. arr = np.array([1, "2", 3, "4"], dtype=object)
  319. msg = "Trying to coerce float values to integers"
  320. with pytest.raises(ValueError, match=msg):
  321. index_cls(arr, dtype=dtype)
  322. def test_constructor_coercion_signed_to_unsigned(
  323. self,
  324. any_unsigned_int_numpy_dtype,
  325. ):
  326. # see gh-15832
  327. msg = "|".join(
  328. [
  329. "Trying to coerce negative values to unsigned integers",
  330. "The elements provided in the data cannot all be casted",
  331. ]
  332. )
  333. with pytest.raises(OverflowError, match=msg):
  334. Index([-1], dtype=any_unsigned_int_numpy_dtype)
  335. def test_constructor_np_signed(self, any_signed_int_numpy_dtype):
  336. # GH#47475
  337. scalar = np.dtype(any_signed_int_numpy_dtype).type(1)
  338. result = Index([scalar])
  339. expected = Index([1], dtype=any_signed_int_numpy_dtype)
  340. tm.assert_index_equal(result, expected, exact=True)
  341. def test_constructor_np_unsigned(self, any_unsigned_int_numpy_dtype):
  342. # GH#47475
  343. scalar = np.dtype(any_unsigned_int_numpy_dtype).type(1)
  344. result = Index([scalar])
  345. expected = Index([1], dtype=any_unsigned_int_numpy_dtype)
  346. tm.assert_index_equal(result, expected, exact=True)
  347. def test_coerce_list(self):
  348. # coerce things
  349. arr = Index([1, 2, 3, 4])
  350. assert isinstance(arr, Index)
  351. # but not if explicit dtype passed
  352. arr = Index([1, 2, 3, 4], dtype=object)
  353. assert type(arr) is Index
  354. class TestFloat16Index:
  355. # float 16 indexes not supported
  356. # GH 49535
  357. def test_constructor(self):
  358. index_cls = Index
  359. dtype = np.float16
  360. msg = "float16 indexes are not supported"
  361. # explicit construction
  362. with pytest.raises(NotImplementedError, match=msg):
  363. index_cls([1, 2, 3, 4, 5], dtype=dtype)
  364. with pytest.raises(NotImplementedError, match=msg):
  365. index_cls(np.array([1, 2, 3, 4, 5]), dtype=dtype)
  366. with pytest.raises(NotImplementedError, match=msg):
  367. index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
  368. with pytest.raises(NotImplementedError, match=msg):
  369. index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
  370. with pytest.raises(NotImplementedError, match=msg):
  371. index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
  372. with pytest.raises(NotImplementedError, match=msg):
  373. index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
  374. # nan handling
  375. with pytest.raises(NotImplementedError, match=msg):
  376. index_cls([np.nan, np.nan], dtype=dtype)
  377. with pytest.raises(NotImplementedError, match=msg):
  378. index_cls(np.array([np.nan]), dtype=dtype)
  379. @pytest.mark.parametrize(
  380. "box",
  381. [list, lambda x: np.array(x, dtype=object), lambda x: Index(x, dtype=object)],
  382. )
  383. def test_uint_index_does_not_convert_to_float64(box):
  384. # https://github.com/pandas-dev/pandas/issues/28279
  385. # https://github.com/pandas-dev/pandas/issues/28023
  386. series = Series(
  387. [0, 1, 2, 3, 4, 5],
  388. index=[
  389. 7606741985629028552,
  390. 17876870360202815256,
  391. 17876870360202815256,
  392. 13106359306506049338,
  393. 8991270399732411471,
  394. 8991270399732411472,
  395. ],
  396. )
  397. result = series.loc[box([7606741985629028552, 17876870360202815256])]
  398. expected = Index(
  399. [7606741985629028552, 17876870360202815256, 17876870360202815256],
  400. dtype="uint64",
  401. )
  402. tm.assert_index_equal(result.index, expected)
  403. tm.assert_equal(result, series.iloc[:3])
  404. def test_float64_index_equals():
  405. # https://github.com/pandas-dev/pandas/issues/35217
  406. float_index = Index([1.0, 2, 3])
  407. string_index = Index(["1", "2", "3"])
  408. result = float_index.equals(string_index)
  409. assert result is False
  410. result = string_index.equals(float_index)
  411. assert result is False
  412. def test_map_dtype_inference_unsigned_to_signed():
  413. # GH#44609 cases where we don't retain dtype
  414. idx = Index([1, 2, 3], dtype=np.uint64)
  415. result = idx.map(lambda x: -x)
  416. expected = Index([-1, -2, -3], dtype=np.int64)
  417. tm.assert_index_equal(result, expected)
  418. def test_map_dtype_inference_overflows():
  419. # GH#44609 case where we have to upcast
  420. idx = Index(np.array([1, 2, 3], dtype=np.int8))
  421. result = idx.map(lambda x: x * 1000)
  422. # TODO: we could plausibly try to infer down to int16 here
  423. expected = Index([1000, 2000, 3000], dtype=np.int64)
  424. tm.assert_index_equal(result, expected)
  425. def test_view_to_datetimelike():
  426. # GH#55710
  427. idx = Index([1, 2, 3])
  428. res = idx.view("m8[s]")
  429. expected = pd.TimedeltaIndex(idx.values.view("m8[s]"))
  430. tm.assert_index_equal(res, expected)
  431. res2 = idx.view("m8[D]")
  432. expected2 = idx.values.view("m8[D]")
  433. tm.assert_numpy_array_equal(res2, expected2)
  434. res3 = idx.view("M8[h]")
  435. expected3 = idx.values.view("M8[h]")
  436. tm.assert_numpy_array_equal(res3, expected3)