test_numeric.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import numpy as np
  4. import pytest
  5. from pandas._libs.tslibs import Timestamp
  6. from pandas.compat import range
  7. import pandas as pd
  8. from pandas import Float64Index, Index, Int64Index, Series, UInt64Index
  9. from pandas.tests.indexes.common import Base
  10. import pandas.util.testing as tm
  11. class Numeric(Base):
  12. def test_can_hold_identifiers(self):
  13. idx = self.create_index()
  14. key = idx[0]
  15. assert idx._can_hold_identifiers_and_holds_name(key) is False
  16. def test_numeric_compat(self):
  17. pass # override Base method
  18. def test_explicit_conversions(self):
  19. # GH 8608
  20. # add/sub are overridden explicitly for Float/Int Index
  21. idx = self._holder(np.arange(5, dtype='int64'))
  22. # float conversions
  23. arr = np.arange(5, dtype='int64') * 3.2
  24. expected = Float64Index(arr)
  25. fidx = idx * 3.2
  26. tm.assert_index_equal(fidx, expected)
  27. fidx = 3.2 * idx
  28. tm.assert_index_equal(fidx, expected)
  29. # interops with numpy arrays
  30. expected = Float64Index(arr)
  31. a = np.zeros(5, dtype='float64')
  32. result = fidx - a
  33. tm.assert_index_equal(result, expected)
  34. expected = Float64Index(-arr)
  35. a = np.zeros(5, dtype='float64')
  36. result = a - fidx
  37. tm.assert_index_equal(result, expected)
  38. def test_index_groupby(self):
  39. int_idx = Index(range(6))
  40. float_idx = Index(np.arange(0, 0.6, 0.1))
  41. obj_idx = Index('A B C D E F'.split())
  42. dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)
  43. for idx in [int_idx, float_idx, obj_idx, dt_idx]:
  44. to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
  45. tm.assert_dict_equal(idx.groupby(to_groupby),
  46. {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})
  47. to_groupby = Index([datetime(2011, 11, 1),
  48. datetime(2011, 12, 1),
  49. pd.NaT,
  50. pd.NaT,
  51. datetime(2011, 12, 1),
  52. datetime(2011, 11, 1)],
  53. tz='UTC').values
  54. ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
  55. expected = {ex_keys[0]: idx[[0, 5]],
  56. ex_keys[1]: idx[[1, 4]]}
  57. tm.assert_dict_equal(idx.groupby(to_groupby), expected)
  58. @pytest.mark.parametrize('klass', [list, tuple, np.array, Series])
  59. def test_where(self, klass):
  60. i = self.create_index()
  61. cond = [True] * len(i)
  62. expected = i
  63. result = i.where(klass(cond))
  64. cond = [False] + [True] * (len(i) - 1)
  65. expected = Float64Index([i._na_value] + i[1:].tolist())
  66. result = i.where(klass(cond))
  67. tm.assert_index_equal(result, expected)
  68. def test_insert(self):
  69. # GH 18295 (test missing)
  70. expected = Float64Index([0, np.nan, 1, 2, 3, 4])
  71. for na in (np.nan, pd.NaT, None):
  72. result = self.create_index().insert(1, na)
  73. tm.assert_index_equal(result, expected)
  74. class TestFloat64Index(Numeric):
  75. _holder = Float64Index
  76. def setup_method(self, method):
  77. self.indices = dict(mixed=Float64Index([1.5, 2, 3, 4, 5]),
  78. float=Float64Index(np.arange(5) * 2.5),
  79. mixed_dec=Float64Index([5, 4, 3, 2, 1.5]),
  80. float_dec=Float64Index(np.arange(4, -1, -1) * 2.5))
  81. self.setup_indices()
  82. def create_index(self):
  83. return Float64Index(np.arange(5, dtype='float64'))
  84. def test_repr_roundtrip(self):
  85. for ind in (self.mixed, self.float):
  86. tm.assert_index_equal(eval(repr(ind)), ind)
  87. def check_is_index(self, i):
  88. assert isinstance(i, Index)
  89. assert not isinstance(i, Float64Index)
  90. def check_coerce(self, a, b, is_float_index=True):
  91. assert a.equals(b)
  92. tm.assert_index_equal(a, b, exact=False)
  93. if is_float_index:
  94. assert isinstance(b, Float64Index)
  95. else:
  96. self.check_is_index(b)
  97. def test_constructor(self):
  98. # explicit construction
  99. index = Float64Index([1, 2, 3, 4, 5])
  100. assert isinstance(index, Float64Index)
  101. expected = np.array([1, 2, 3, 4, 5], dtype='float64')
  102. tm.assert_numpy_array_equal(index.values, expected)
  103. index = Float64Index(np.array([1, 2, 3, 4, 5]))
  104. assert isinstance(index, Float64Index)
  105. index = Float64Index([1., 2, 3, 4, 5])
  106. assert isinstance(index, Float64Index)
  107. index = Float64Index(np.array([1., 2, 3, 4, 5]))
  108. assert isinstance(index, Float64Index)
  109. assert index.dtype == float
  110. index = Float64Index(np.array([1., 2, 3, 4, 5]), dtype=np.float32)
  111. assert isinstance(index, Float64Index)
  112. assert index.dtype == np.float64
  113. index = Float64Index(np.array([1, 2, 3, 4, 5]), dtype=np.float32)
  114. assert isinstance(index, Float64Index)
  115. assert index.dtype == np.float64
  116. # nan handling
  117. result = Float64Index([np.nan, np.nan])
  118. assert pd.isna(result.values).all()
  119. result = Float64Index(np.array([np.nan]))
  120. assert pd.isna(result.values).all()
  121. result = Index(np.array([np.nan]))
  122. assert pd.isna(result.values).all()
  123. def test_constructor_invalid(self):
  124. # invalid
  125. pytest.raises(TypeError, Float64Index, 0.)
  126. pytest.raises(TypeError, Float64Index, ['a', 'b', 0.])
  127. pytest.raises(TypeError, Float64Index, [Timestamp('20130101')])
  128. def test_constructor_coerce(self):
  129. self.check_coerce(self.mixed, Index([1.5, 2, 3, 4, 5]))
  130. self.check_coerce(self.float, Index(np.arange(5) * 2.5))
  131. self.check_coerce(self.float, Index(np.array(
  132. np.arange(5) * 2.5, dtype=object)))
  133. def test_constructor_explicit(self):
  134. # these don't auto convert
  135. self.check_coerce(self.float,
  136. Index((np.arange(5) * 2.5), dtype=object),
  137. is_float_index=False)
  138. self.check_coerce(self.mixed, Index(
  139. [1.5, 2, 3, 4, 5], dtype=object), is_float_index=False)
  140. def test_astype(self):
  141. result = self.float.astype(object)
  142. assert result.equals(self.float)
  143. assert self.float.equals(result)
  144. self.check_is_index(result)
  145. i = self.mixed.copy()
  146. i.name = 'foo'
  147. result = i.astype(object)
  148. assert result.equals(i)
  149. assert i.equals(result)
  150. self.check_is_index(result)
  151. # GH 12881
  152. # a float astype int
  153. for dtype in ['int16', 'int32', 'int64']:
  154. i = Float64Index([0, 1, 2])
  155. result = i.astype(dtype)
  156. expected = Int64Index([0, 1, 2])
  157. tm.assert_index_equal(result, expected)
  158. i = Float64Index([0, 1.1, 2])
  159. result = i.astype(dtype)
  160. expected = Int64Index([0, 1, 2])
  161. tm.assert_index_equal(result, expected)
  162. for dtype in ['float32', 'float64']:
  163. i = Float64Index([0, 1, 2])
  164. result = i.astype(dtype)
  165. expected = i
  166. tm.assert_index_equal(result, expected)
  167. i = Float64Index([0, 1.1, 2])
  168. result = i.astype(dtype)
  169. expected = Index(i.values.astype(dtype))
  170. tm.assert_index_equal(result, expected)
  171. # invalid
  172. for dtype in ['M8[ns]', 'm8[ns]']:
  173. pytest.raises(TypeError, lambda: i.astype(dtype))
  174. # GH 13149
  175. for dtype in ['int16', 'int32', 'int64']:
  176. i = Float64Index([0, 1.1, np.NAN])
  177. pytest.raises(ValueError, lambda: i.astype(dtype))
  178. def test_type_coercion_fail(self, any_int_dtype):
  179. # see gh-15832
  180. msg = "Trying to coerce float values to integers"
  181. with pytest.raises(ValueError, match=msg):
  182. Index([1, 2, 3.5], dtype=any_int_dtype)
  183. def test_type_coercion_valid(self, float_dtype):
  184. # There is no Float32Index, so we always
  185. # generate Float64Index.
  186. i = Index([1, 2, 3.5], dtype=float_dtype)
  187. tm.assert_index_equal(i, Index([1, 2, 3.5]))
  188. def test_equals_numeric(self):
  189. i = Float64Index([1.0, 2.0])
  190. assert i.equals(i)
  191. assert i.identical(i)
  192. i2 = Float64Index([1.0, 2.0])
  193. assert i.equals(i2)
  194. i = Float64Index([1.0, np.nan])
  195. assert i.equals(i)
  196. assert i.identical(i)
  197. i2 = Float64Index([1.0, np.nan])
  198. assert i.equals(i2)
  199. def test_get_indexer(self):
  200. idx = Float64Index([0.0, 1.0, 2.0])
  201. tm.assert_numpy_array_equal(idx.get_indexer(idx),
  202. np.array([0, 1, 2], dtype=np.intp))
  203. target = [-0.1, 0.5, 1.1]
  204. tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'),
  205. np.array([-1, 0, 1], dtype=np.intp))
  206. tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'),
  207. np.array([0, 1, 2], dtype=np.intp))
  208. tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'),
  209. np.array([0, 1, 1], dtype=np.intp))
  210. def test_get_loc(self):
  211. idx = Float64Index([0.0, 1.0, 2.0])
  212. for method in [None, 'pad', 'backfill', 'nearest']:
  213. assert idx.get_loc(1, method) == 1
  214. if method is not None:
  215. assert idx.get_loc(1, method, tolerance=0) == 1
  216. for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
  217. assert idx.get_loc(1.1, method) == loc
  218. assert idx.get_loc(1.1, method, tolerance=0.9) == loc
  219. pytest.raises(KeyError, idx.get_loc, 'foo')
  220. pytest.raises(KeyError, idx.get_loc, 1.5)
  221. pytest.raises(KeyError, idx.get_loc, 1.5, method='pad',
  222. tolerance=0.1)
  223. pytest.raises(KeyError, idx.get_loc, True)
  224. pytest.raises(KeyError, idx.get_loc, False)
  225. with pytest.raises(ValueError, match='must be numeric'):
  226. idx.get_loc(1.4, method='nearest', tolerance='foo')
  227. with pytest.raises(ValueError, match='must contain numeric elements'):
  228. idx.get_loc(1.4, method='nearest', tolerance=np.array(['foo']))
  229. with pytest.raises(
  230. ValueError,
  231. match='tolerance size must match target index size'):
  232. idx.get_loc(1.4, method='nearest', tolerance=np.array([1, 2]))
  233. def test_get_loc_na(self):
  234. idx = Float64Index([np.nan, 1, 2])
  235. assert idx.get_loc(1) == 1
  236. assert idx.get_loc(np.nan) == 0
  237. idx = Float64Index([np.nan, 1, np.nan])
  238. assert idx.get_loc(1) == 1
  239. # representable by slice [0:2:2]
  240. # pytest.raises(KeyError, idx.slice_locs, np.nan)
  241. sliced = idx.slice_locs(np.nan)
  242. assert isinstance(sliced, tuple)
  243. assert sliced == (0, 3)
  244. # not representable by slice
  245. idx = Float64Index([np.nan, 1, np.nan, np.nan])
  246. assert idx.get_loc(1) == 1
  247. pytest.raises(KeyError, idx.slice_locs, np.nan)
  248. def test_get_loc_missing_nan(self):
  249. # GH 8569
  250. idx = Float64Index([1, 2])
  251. assert idx.get_loc(1) == 0
  252. pytest.raises(KeyError, idx.get_loc, 3)
  253. pytest.raises(KeyError, idx.get_loc, np.nan)
  254. pytest.raises(KeyError, idx.get_loc, [np.nan])
  255. def test_contains_nans(self):
  256. i = Float64Index([1.0, 2.0, np.nan])
  257. assert np.nan in i
  258. def test_contains_not_nans(self):
  259. i = Float64Index([1.0, 2.0, np.nan])
  260. assert 1.0 in i
  261. def test_doesnt_contain_all_the_things(self):
  262. i = Float64Index([np.nan])
  263. assert not i.isin([0]).item()
  264. assert not i.isin([1]).item()
  265. assert i.isin([np.nan]).item()
  266. def test_nan_multiple_containment(self):
  267. i = Float64Index([1.0, np.nan])
  268. tm.assert_numpy_array_equal(i.isin([1.0]), np.array([True, False]))
  269. tm.assert_numpy_array_equal(i.isin([2.0, np.pi]),
  270. np.array([False, False]))
  271. tm.assert_numpy_array_equal(i.isin([np.nan]), np.array([False, True]))
  272. tm.assert_numpy_array_equal(i.isin([1.0, np.nan]),
  273. np.array([True, True]))
  274. i = Float64Index([1.0, 2.0])
  275. tm.assert_numpy_array_equal(i.isin([np.nan]), np.array([False, False]))
  276. def test_astype_from_object(self):
  277. index = Index([1.0, np.nan, 0.2], dtype='object')
  278. result = index.astype(float)
  279. expected = Float64Index([1.0, np.nan, 0.2])
  280. assert result.dtype == expected.dtype
  281. tm.assert_index_equal(result, expected)
  282. def test_fillna_float64(self):
  283. # GH 11343
  284. idx = Index([1.0, np.nan, 3.0], dtype=float, name='x')
  285. # can't downcast
  286. exp = Index([1.0, 0.1, 3.0], name='x')
  287. tm.assert_index_equal(idx.fillna(0.1), exp)
  288. # downcast
  289. exp = Float64Index([1.0, 2.0, 3.0], name='x')
  290. tm.assert_index_equal(idx.fillna(2), exp)
  291. # object
  292. exp = Index([1.0, 'obj', 3.0], name='x')
  293. tm.assert_index_equal(idx.fillna('obj'), exp)
  294. def test_take_fill_value(self):
  295. # GH 12631
  296. idx = pd.Float64Index([1., 2., 3.], name='xxx')
  297. result = idx.take(np.array([1, 0, -1]))
  298. expected = pd.Float64Index([2., 1., 3.], name='xxx')
  299. tm.assert_index_equal(result, expected)
  300. # fill_value
  301. result = idx.take(np.array([1, 0, -1]), fill_value=True)
  302. expected = pd.Float64Index([2., 1., np.nan], name='xxx')
  303. tm.assert_index_equal(result, expected)
  304. # allow_fill=False
  305. result = idx.take(np.array([1, 0, -1]), allow_fill=False,
  306. fill_value=True)
  307. expected = pd.Float64Index([2., 1., 3.], name='xxx')
  308. tm.assert_index_equal(result, expected)
  309. msg = ('When allow_fill=True and fill_value is not None, '
  310. 'all indices must be >= -1')
  311. with pytest.raises(ValueError, match=msg):
  312. idx.take(np.array([1, 0, -2]), fill_value=True)
  313. with pytest.raises(ValueError, match=msg):
  314. idx.take(np.array([1, 0, -5]), fill_value=True)
  315. with pytest.raises(IndexError):
  316. idx.take(np.array([1, -5]))
  317. class NumericInt(Numeric):
  318. def test_view(self):
  319. i = self._holder([], name='Foo')
  320. i_view = i.view()
  321. assert i_view.name == 'Foo'
  322. i_view = i.view(self._dtype)
  323. tm.assert_index_equal(i, self._holder(i_view, name='Foo'))
  324. i_view = i.view(self._holder)
  325. tm.assert_index_equal(i, self._holder(i_view, name='Foo'))
  326. def test_is_monotonic(self):
  327. assert self.index.is_monotonic is True
  328. assert self.index.is_monotonic_increasing is True
  329. assert self.index._is_strictly_monotonic_increasing is True
  330. assert self.index.is_monotonic_decreasing is False
  331. assert self.index._is_strictly_monotonic_decreasing is False
  332. index = self._holder([4, 3, 2, 1])
  333. assert index.is_monotonic is False
  334. assert index._is_strictly_monotonic_increasing is False
  335. assert index._is_strictly_monotonic_decreasing is True
  336. index = self._holder([1])
  337. assert index.is_monotonic is True
  338. assert index.is_monotonic_increasing is True
  339. assert index.is_monotonic_decreasing is True
  340. assert index._is_strictly_monotonic_increasing is True
  341. assert index._is_strictly_monotonic_decreasing is True
  342. def test_is_strictly_monotonic(self):
  343. index = self._holder([1, 1, 2, 3])
  344. assert index.is_monotonic_increasing is True
  345. assert index._is_strictly_monotonic_increasing is False
  346. index = self._holder([3, 2, 1, 1])
  347. assert index.is_monotonic_decreasing is True
  348. assert index._is_strictly_monotonic_decreasing is False
  349. index = self._holder([1, 1])
  350. assert index.is_monotonic_increasing
  351. assert index.is_monotonic_decreasing
  352. assert not index._is_strictly_monotonic_increasing
  353. assert not index._is_strictly_monotonic_decreasing
  354. def test_logical_compat(self):
  355. idx = self.create_index()
  356. assert idx.all() == idx.values.all()
  357. assert idx.any() == idx.values.any()
  358. def test_identical(self):
  359. i = Index(self.index.copy())
  360. assert i.identical(self.index)
  361. same_values_different_type = Index(i, dtype=object)
  362. assert not i.identical(same_values_different_type)
  363. i = self.index.copy(dtype=object)
  364. i = i.rename('foo')
  365. same_values = Index(i, dtype=object)
  366. assert same_values.identical(i)
  367. assert not i.identical(self.index)
  368. assert Index(same_values, name='foo', dtype=object).identical(i)
  369. assert not self.index.copy(dtype=object).identical(
  370. self.index.copy(dtype=self._dtype))
  371. def test_join_non_unique(self):
  372. left = Index([4, 4, 3, 3])
  373. joined, lidx, ridx = left.join(left, return_indexers=True)
  374. exp_joined = Index([3, 3, 3, 3, 4, 4, 4, 4])
  375. tm.assert_index_equal(joined, exp_joined)
  376. exp_lidx = np.array([2, 2, 3, 3, 0, 0, 1, 1], dtype=np.intp)
  377. tm.assert_numpy_array_equal(lidx, exp_lidx)
  378. exp_ridx = np.array([2, 3, 2, 3, 0, 1, 0, 1], dtype=np.intp)
  379. tm.assert_numpy_array_equal(ridx, exp_ridx)
  380. @pytest.mark.parametrize('kind', ['outer', 'inner', 'left', 'right'])
  381. def test_join_self(self, kind):
  382. joined = self.index.join(self.index, how=kind)
  383. assert self.index is joined
  384. def test_union_noncomparable(self):
  385. from datetime import datetime, timedelta
  386. # corner case, non-Int64Index
  387. now = datetime.now()
  388. other = Index([now + timedelta(i) for i in range(4)], dtype=object)
  389. result = self.index.union(other)
  390. expected = Index(np.concatenate((self.index, other)))
  391. tm.assert_index_equal(result, expected)
  392. result = other.union(self.index)
  393. expected = Index(np.concatenate((other, self.index)))
  394. tm.assert_index_equal(result, expected)
  395. def test_cant_or_shouldnt_cast(self):
  396. # can't
  397. data = ['foo', 'bar', 'baz']
  398. pytest.raises(TypeError, self._holder, data)
  399. # shouldn't
  400. data = ['0', '1', '2']
  401. pytest.raises(TypeError, self._holder, data)
  402. def test_view_index(self):
  403. self.index.view(Index)
  404. def test_prevent_casting(self):
  405. result = self.index.astype('O')
  406. assert result.dtype == np.object_
  407. def test_take_preserve_name(self):
  408. index = self._holder([1, 2, 3, 4], name='foo')
  409. taken = index.take([3, 0, 1])
  410. assert index.name == taken.name
  411. def test_take_fill_value(self):
  412. # see gh-12631
  413. idx = self._holder([1, 2, 3], name='xxx')
  414. result = idx.take(np.array([1, 0, -1]))
  415. expected = self._holder([2, 1, 3], name='xxx')
  416. tm.assert_index_equal(result, expected)
  417. name = self._holder.__name__
  418. msg = ("Unable to fill values because "
  419. "{name} cannot contain NA").format(name=name)
  420. # fill_value=True
  421. with pytest.raises(ValueError, match=msg):
  422. idx.take(np.array([1, 0, -1]), fill_value=True)
  423. # allow_fill=False
  424. result = idx.take(np.array([1, 0, -1]), allow_fill=False,
  425. fill_value=True)
  426. expected = self._holder([2, 1, 3], name='xxx')
  427. tm.assert_index_equal(result, expected)
  428. with pytest.raises(ValueError, match=msg):
  429. idx.take(np.array([1, 0, -2]), fill_value=True)
  430. with pytest.raises(ValueError, match=msg):
  431. idx.take(np.array([1, 0, -5]), fill_value=True)
  432. with pytest.raises(IndexError):
  433. idx.take(np.array([1, -5]))
  434. def test_slice_keep_name(self):
  435. idx = self._holder([1, 2], name='asdf')
  436. assert idx.name == idx[1:].name
  437. class TestInt64Index(NumericInt):
  438. _dtype = 'int64'
  439. _holder = Int64Index
  440. def setup_method(self, method):
  441. self.indices = dict(index=Int64Index(np.arange(0, 20, 2)),
  442. index_dec=Int64Index(np.arange(19, -1, -1)))
  443. self.setup_indices()
  444. def create_index(self):
  445. return Int64Index(np.arange(5, dtype='int64'))
  446. def test_constructor(self):
  447. # pass list, coerce fine
  448. index = Int64Index([-5, 0, 1, 2])
  449. expected = Index([-5, 0, 1, 2], dtype=np.int64)
  450. tm.assert_index_equal(index, expected)
  451. # from iterable
  452. index = Int64Index(iter([-5, 0, 1, 2]))
  453. tm.assert_index_equal(index, expected)
  454. # scalar raise Exception
  455. pytest.raises(TypeError, Int64Index, 5)
  456. # copy
  457. arr = self.index.values
  458. new_index = Int64Index(arr, copy=True)
  459. tm.assert_index_equal(new_index, self.index)
  460. val = arr[0] + 3000
  461. # this should not change index
  462. arr[0] = val
  463. assert new_index[0] != val
  464. # interpret list-like
  465. expected = Int64Index([5, 0])
  466. for cls in [Index, Int64Index]:
  467. for idx in [cls([5, 0], dtype='int64'),
  468. cls(np.array([5, 0]), dtype='int64'),
  469. cls(Series([5, 0]), dtype='int64')]:
  470. tm.assert_index_equal(idx, expected)
  471. def test_constructor_corner(self):
  472. arr = np.array([1, 2, 3, 4], dtype=object)
  473. index = Int64Index(arr)
  474. assert index.values.dtype == np.int64
  475. tm.assert_index_equal(index, Index(arr))
  476. # preventing casting
  477. arr = np.array([1, '2', 3, '4'], dtype=object)
  478. with pytest.raises(TypeError, match='casting'):
  479. Int64Index(arr)
  480. arr_with_floats = [0, 2, 3, 4, 5, 1.25, 3, -1]
  481. with pytest.raises(TypeError, match='casting'):
  482. Int64Index(arr_with_floats)
  483. def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
  484. # see gh-15832
  485. msg = "Trying to coerce negative values to unsigned integers"
  486. with pytest.raises(OverflowError, match=msg):
  487. Index([-1], dtype=uint_dtype)
  488. def test_constructor_unwraps_index(self):
  489. idx = pd.Index([1, 2])
  490. result = pd.Int64Index(idx)
  491. expected = np.array([1, 2], dtype='int64')
  492. tm.assert_numpy_array_equal(result._data, expected)
  493. def test_coerce_list(self):
  494. # coerce things
  495. arr = Index([1, 2, 3, 4])
  496. assert isinstance(arr, Int64Index)
  497. # but not if explicit dtype passed
  498. arr = Index([1, 2, 3, 4], dtype=object)
  499. assert isinstance(arr, Index)
  500. def test_get_indexer(self):
  501. target = Int64Index(np.arange(10))
  502. indexer = self.index.get_indexer(target)
  503. expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
  504. tm.assert_numpy_array_equal(indexer, expected)
  505. target = Int64Index(np.arange(10))
  506. indexer = self.index.get_indexer(target, method='pad')
  507. expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
  508. tm.assert_numpy_array_equal(indexer, expected)
  509. target = Int64Index(np.arange(10))
  510. indexer = self.index.get_indexer(target, method='backfill')
  511. expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
  512. tm.assert_numpy_array_equal(indexer, expected)
  513. def test_intersection(self):
  514. other = Index([1, 2, 3, 4, 5])
  515. result = self.index.intersection(other)
  516. expected = Index(np.sort(np.intersect1d(self.index.values,
  517. other.values)))
  518. tm.assert_index_equal(result, expected)
  519. result = other.intersection(self.index)
  520. expected = Index(np.sort(np.asarray(np.intersect1d(self.index.values,
  521. other.values))))
  522. tm.assert_index_equal(result, expected)
  523. def test_join_inner(self):
  524. other = Int64Index([7, 12, 25, 1, 2, 5])
  525. other_mono = Int64Index([1, 2, 5, 7, 12, 25])
  526. # not monotonic
  527. res, lidx, ridx = self.index.join(other, how='inner',
  528. return_indexers=True)
  529. # no guarantee of sortedness, so sort for comparison purposes
  530. ind = res.argsort()
  531. res = res.take(ind)
  532. lidx = lidx.take(ind)
  533. ridx = ridx.take(ind)
  534. eres = Int64Index([2, 12])
  535. elidx = np.array([1, 6], dtype=np.intp)
  536. eridx = np.array([4, 1], dtype=np.intp)
  537. assert isinstance(res, Int64Index)
  538. tm.assert_index_equal(res, eres)
  539. tm.assert_numpy_array_equal(lidx, elidx)
  540. tm.assert_numpy_array_equal(ridx, eridx)
  541. # monotonic
  542. res, lidx, ridx = self.index.join(other_mono, how='inner',
  543. return_indexers=True)
  544. res2 = self.index.intersection(other_mono)
  545. tm.assert_index_equal(res, res2)
  546. elidx = np.array([1, 6], dtype=np.intp)
  547. eridx = np.array([1, 4], dtype=np.intp)
  548. assert isinstance(res, Int64Index)
  549. tm.assert_index_equal(res, eres)
  550. tm.assert_numpy_array_equal(lidx, elidx)
  551. tm.assert_numpy_array_equal(ridx, eridx)
  552. def test_join_left(self):
  553. other = Int64Index([7, 12, 25, 1, 2, 5])
  554. other_mono = Int64Index([1, 2, 5, 7, 12, 25])
  555. # not monotonic
  556. res, lidx, ridx = self.index.join(other, how='left',
  557. return_indexers=True)
  558. eres = self.index
  559. eridx = np.array([-1, 4, -1, -1, -1, -1, 1, -1, -1, -1],
  560. dtype=np.intp)
  561. assert isinstance(res, Int64Index)
  562. tm.assert_index_equal(res, eres)
  563. assert lidx is None
  564. tm.assert_numpy_array_equal(ridx, eridx)
  565. # monotonic
  566. res, lidx, ridx = self.index.join(other_mono, how='left',
  567. return_indexers=True)
  568. eridx = np.array([-1, 1, -1, -1, -1, -1, 4, -1, -1, -1],
  569. dtype=np.intp)
  570. assert isinstance(res, Int64Index)
  571. tm.assert_index_equal(res, eres)
  572. assert lidx is None
  573. tm.assert_numpy_array_equal(ridx, eridx)
  574. # non-unique
  575. idx = Index([1, 1, 2, 5])
  576. idx2 = Index([1, 2, 5, 7, 9])
  577. res, lidx, ridx = idx2.join(idx, how='left', return_indexers=True)
  578. eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
  579. eridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
  580. elidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
  581. tm.assert_index_equal(res, eres)
  582. tm.assert_numpy_array_equal(lidx, elidx)
  583. tm.assert_numpy_array_equal(ridx, eridx)
  584. def test_join_right(self):
  585. other = Int64Index([7, 12, 25, 1, 2, 5])
  586. other_mono = Int64Index([1, 2, 5, 7, 12, 25])
  587. # not monotonic
  588. res, lidx, ridx = self.index.join(other, how='right',
  589. return_indexers=True)
  590. eres = other
  591. elidx = np.array([-1, 6, -1, -1, 1, -1], dtype=np.intp)
  592. assert isinstance(other, Int64Index)
  593. tm.assert_index_equal(res, eres)
  594. tm.assert_numpy_array_equal(lidx, elidx)
  595. assert ridx is None
  596. # monotonic
  597. res, lidx, ridx = self.index.join(other_mono, how='right',
  598. return_indexers=True)
  599. eres = other_mono
  600. elidx = np.array([-1, 1, -1, -1, 6, -1], dtype=np.intp)
  601. assert isinstance(other, Int64Index)
  602. tm.assert_index_equal(res, eres)
  603. tm.assert_numpy_array_equal(lidx, elidx)
  604. assert ridx is None
  605. # non-unique
  606. idx = Index([1, 1, 2, 5])
  607. idx2 = Index([1, 2, 5, 7, 9])
  608. res, lidx, ridx = idx.join(idx2, how='right', return_indexers=True)
  609. eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
  610. elidx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
  611. eridx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
  612. tm.assert_index_equal(res, eres)
  613. tm.assert_numpy_array_equal(lidx, elidx)
  614. tm.assert_numpy_array_equal(ridx, eridx)
  615. def test_join_non_int_index(self):
  616. other = Index([3, 6, 7, 8, 10], dtype=object)
  617. outer = self.index.join(other, how='outer')
  618. outer2 = other.join(self.index, how='outer')
  619. expected = Index([0, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18])
  620. tm.assert_index_equal(outer, outer2)
  621. tm.assert_index_equal(outer, expected)
  622. inner = self.index.join(other, how='inner')
  623. inner2 = other.join(self.index, how='inner')
  624. expected = Index([6, 8, 10])
  625. tm.assert_index_equal(inner, inner2)
  626. tm.assert_index_equal(inner, expected)
  627. left = self.index.join(other, how='left')
  628. tm.assert_index_equal(left, self.index.astype(object))
  629. left2 = other.join(self.index, how='left')
  630. tm.assert_index_equal(left2, other)
  631. right = self.index.join(other, how='right')
  632. tm.assert_index_equal(right, other)
  633. right2 = other.join(self.index, how='right')
  634. tm.assert_index_equal(right2, self.index.astype(object))
  635. def test_join_outer(self):
  636. other = Int64Index([7, 12, 25, 1, 2, 5])
  637. other_mono = Int64Index([1, 2, 5, 7, 12, 25])
  638. # not monotonic
  639. # guarantee of sortedness
  640. res, lidx, ridx = self.index.join(other, how='outer',
  641. return_indexers=True)
  642. noidx_res = self.index.join(other, how='outer')
  643. tm.assert_index_equal(res, noidx_res)
  644. eres = Int64Index([0, 1, 2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 25])
  645. elidx = np.array([0, -1, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, 9, -1],
  646. dtype=np.intp)
  647. eridx = np.array([-1, 3, 4, -1, 5, -1, 0, -1, -1, 1, -1, -1, -1, 2],
  648. dtype=np.intp)
  649. assert isinstance(res, Int64Index)
  650. tm.assert_index_equal(res, eres)
  651. tm.assert_numpy_array_equal(lidx, elidx)
  652. tm.assert_numpy_array_equal(ridx, eridx)
  653. # monotonic
  654. res, lidx, ridx = self.index.join(other_mono, how='outer',
  655. return_indexers=True)
  656. noidx_res = self.index.join(other_mono, how='outer')
  657. tm.assert_index_equal(res, noidx_res)
  658. elidx = np.array([0, -1, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, 9, -1],
  659. dtype=np.intp)
  660. eridx = np.array([-1, 0, 1, -1, 2, -1, 3, -1, -1, 4, -1, -1, -1, 5],
  661. dtype=np.intp)
  662. assert isinstance(res, Int64Index)
  663. tm.assert_index_equal(res, eres)
  664. tm.assert_numpy_array_equal(lidx, elidx)
  665. tm.assert_numpy_array_equal(ridx, eridx)
  666. class TestUInt64Index(NumericInt):
  667. _dtype = 'uint64'
  668. _holder = UInt64Index
  669. def setup_method(self, method):
  670. vals = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
  671. self.indices = dict(index=UInt64Index(vals),
  672. index_dec=UInt64Index(reversed(vals)))
  673. self.setup_indices()
  674. def create_index(self):
  675. return UInt64Index(np.arange(5, dtype='uint64'))
  676. def test_constructor(self):
  677. idx = UInt64Index([1, 2, 3])
  678. res = Index([1, 2, 3], dtype=np.uint64)
  679. tm.assert_index_equal(res, idx)
  680. idx = UInt64Index([1, 2**63])
  681. res = Index([1, 2**63], dtype=np.uint64)
  682. tm.assert_index_equal(res, idx)
  683. idx = UInt64Index([1, 2**63])
  684. res = Index([1, 2**63])
  685. tm.assert_index_equal(res, idx)
  686. idx = Index([-1, 2**63], dtype=object)
  687. res = Index(np.array([-1, 2**63], dtype=object))
  688. tm.assert_index_equal(res, idx)
  689. def test_get_indexer(self):
  690. target = UInt64Index(np.arange(10).astype('uint64') * 5 + 2**63)
  691. indexer = self.index.get_indexer(target)
  692. expected = np.array([0, -1, 1, 2, 3, 4,
  693. -1, -1, -1, -1], dtype=np.intp)
  694. tm.assert_numpy_array_equal(indexer, expected)
  695. target = UInt64Index(np.arange(10).astype('uint64') * 5 + 2**63)
  696. indexer = self.index.get_indexer(target, method='pad')
  697. expected = np.array([0, 0, 1, 2, 3, 4,
  698. 4, 4, 4, 4], dtype=np.intp)
  699. tm.assert_numpy_array_equal(indexer, expected)
  700. target = UInt64Index(np.arange(10).astype('uint64') * 5 + 2**63)
  701. indexer = self.index.get_indexer(target, method='backfill')
  702. expected = np.array([0, 1, 1, 2, 3, 4,
  703. -1, -1, -1, -1], dtype=np.intp)
  704. tm.assert_numpy_array_equal(indexer, expected)
  705. def test_intersection(self):
  706. other = Index([2**63, 2**63 + 5, 2**63 + 10, 2**63 + 15, 2**63 + 20])
  707. result = self.index.intersection(other)
  708. expected = Index(np.sort(np.intersect1d(self.index.values,
  709. other.values)))
  710. tm.assert_index_equal(result, expected)
  711. result = other.intersection(self.index)
  712. expected = Index(np.sort(np.asarray(np.intersect1d(self.index.values,
  713. other.values))))
  714. tm.assert_index_equal(result, expected)
  715. def test_join_inner(self):
  716. other = UInt64Index(2**63 + np.array(
  717. [7, 12, 25, 1, 2, 10], dtype='uint64'))
  718. other_mono = UInt64Index(2**63 + np.array(
  719. [1, 2, 7, 10, 12, 25], dtype='uint64'))
  720. # not monotonic
  721. res, lidx, ridx = self.index.join(other, how='inner',
  722. return_indexers=True)
  723. # no guarantee of sortedness, so sort for comparison purposes
  724. ind = res.argsort()
  725. res = res.take(ind)
  726. lidx = lidx.take(ind)
  727. ridx = ridx.take(ind)
  728. eres = UInt64Index(2**63 + np.array([10, 25], dtype='uint64'))
  729. elidx = np.array([1, 4], dtype=np.intp)
  730. eridx = np.array([5, 2], dtype=np.intp)
  731. assert isinstance(res, UInt64Index)
  732. tm.assert_index_equal(res, eres)
  733. tm.assert_numpy_array_equal(lidx, elidx)
  734. tm.assert_numpy_array_equal(ridx, eridx)
  735. # monotonic
  736. res, lidx, ridx = self.index.join(other_mono, how='inner',
  737. return_indexers=True)
  738. res2 = self.index.intersection(other_mono)
  739. tm.assert_index_equal(res, res2)
  740. elidx = np.array([1, 4], dtype=np.intp)
  741. eridx = np.array([3, 5], dtype=np.intp)
  742. assert isinstance(res, UInt64Index)
  743. tm.assert_index_equal(res, eres)
  744. tm.assert_numpy_array_equal(lidx, elidx)
  745. tm.assert_numpy_array_equal(ridx, eridx)
  746. def test_join_left(self):
  747. other = UInt64Index(2**63 + np.array(
  748. [7, 12, 25, 1, 2, 10], dtype='uint64'))
  749. other_mono = UInt64Index(2**63 + np.array(
  750. [1, 2, 7, 10, 12, 25], dtype='uint64'))
  751. # not monotonic
  752. res, lidx, ridx = self.index.join(other, how='left',
  753. return_indexers=True)
  754. eres = self.index
  755. eridx = np.array([-1, 5, -1, -1, 2], dtype=np.intp)
  756. assert isinstance(res, UInt64Index)
  757. tm.assert_index_equal(res, eres)
  758. assert lidx is None
  759. tm.assert_numpy_array_equal(ridx, eridx)
  760. # monotonic
  761. res, lidx, ridx = self.index.join(other_mono, how='left',
  762. return_indexers=True)
  763. eridx = np.array([-1, 3, -1, -1, 5], dtype=np.intp)
  764. assert isinstance(res, UInt64Index)
  765. tm.assert_index_equal(res, eres)
  766. assert lidx is None
  767. tm.assert_numpy_array_equal(ridx, eridx)
  768. # non-unique
  769. idx = UInt64Index(2**63 + np.array([1, 1, 2, 5], dtype='uint64'))
  770. idx2 = UInt64Index(2**63 + np.array([1, 2, 5, 7, 9], dtype='uint64'))
  771. res, lidx, ridx = idx2.join(idx, how='left', return_indexers=True)
  772. # 1 is in idx2, so it should be x2
  773. eres = UInt64Index(2**63 + np.array(
  774. [1, 1, 2, 5, 7, 9], dtype='uint64'))
  775. eridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
  776. elidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
  777. tm.assert_index_equal(res, eres)
  778. tm.assert_numpy_array_equal(lidx, elidx)
  779. tm.assert_numpy_array_equal(ridx, eridx)
  780. def test_join_right(self):
  781. other = UInt64Index(2**63 + np.array(
  782. [7, 12, 25, 1, 2, 10], dtype='uint64'))
  783. other_mono = UInt64Index(2**63 + np.array(
  784. [1, 2, 7, 10, 12, 25], dtype='uint64'))
  785. # not monotonic
  786. res, lidx, ridx = self.index.join(other, how='right',
  787. return_indexers=True)
  788. eres = other
  789. elidx = np.array([-1, -1, 4, -1, -1, 1], dtype=np.intp)
  790. tm.assert_numpy_array_equal(lidx, elidx)
  791. assert isinstance(other, UInt64Index)
  792. tm.assert_index_equal(res, eres)
  793. assert ridx is None
  794. # monotonic
  795. res, lidx, ridx = self.index.join(other_mono, how='right',
  796. return_indexers=True)
  797. eres = other_mono
  798. elidx = np.array([-1, -1, -1, 1, -1, 4], dtype=np.intp)
  799. assert isinstance(other, UInt64Index)
  800. tm.assert_numpy_array_equal(lidx, elidx)
  801. tm.assert_index_equal(res, eres)
  802. assert ridx is None
  803. # non-unique
  804. idx = UInt64Index(2**63 + np.array([1, 1, 2, 5], dtype='uint64'))
  805. idx2 = UInt64Index(2**63 + np.array([1, 2, 5, 7, 9], dtype='uint64'))
  806. res, lidx, ridx = idx.join(idx2, how='right', return_indexers=True)
  807. # 1 is in idx2, so it should be x2
  808. eres = UInt64Index(2**63 + np.array(
  809. [1, 1, 2, 5, 7, 9], dtype='uint64'))
  810. elidx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
  811. eridx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
  812. tm.assert_index_equal(res, eres)
  813. tm.assert_numpy_array_equal(lidx, elidx)
  814. tm.assert_numpy_array_equal(ridx, eridx)
  815. def test_join_non_int_index(self):
  816. other = Index(2**63 + np.array(
  817. [1, 5, 7, 10, 20], dtype='uint64'), dtype=object)
  818. outer = self.index.join(other, how='outer')
  819. outer2 = other.join(self.index, how='outer')
  820. expected = Index(2**63 + np.array(
  821. [0, 1, 5, 7, 10, 15, 20, 25], dtype='uint64'))
  822. tm.assert_index_equal(outer, outer2)
  823. tm.assert_index_equal(outer, expected)
  824. inner = self.index.join(other, how='inner')
  825. inner2 = other.join(self.index, how='inner')
  826. expected = Index(2**63 + np.array([10, 20], dtype='uint64'))
  827. tm.assert_index_equal(inner, inner2)
  828. tm.assert_index_equal(inner, expected)
  829. left = self.index.join(other, how='left')
  830. tm.assert_index_equal(left, self.index.astype(object))
  831. left2 = other.join(self.index, how='left')
  832. tm.assert_index_equal(left2, other)
  833. right = self.index.join(other, how='right')
  834. tm.assert_index_equal(right, other)
  835. right2 = other.join(self.index, how='right')
  836. tm.assert_index_equal(right2, self.index.astype(object))
  837. def test_join_outer(self):
  838. other = UInt64Index(2**63 + np.array(
  839. [7, 12, 25, 1, 2, 10], dtype='uint64'))
  840. other_mono = UInt64Index(2**63 + np.array(
  841. [1, 2, 7, 10, 12, 25], dtype='uint64'))
  842. # not monotonic
  843. # guarantee of sortedness
  844. res, lidx, ridx = self.index.join(other, how='outer',
  845. return_indexers=True)
  846. noidx_res = self.index.join(other, how='outer')
  847. tm.assert_index_equal(res, noidx_res)
  848. eres = UInt64Index(2**63 + np.array(
  849. [0, 1, 2, 7, 10, 12, 15, 20, 25], dtype='uint64'))
  850. elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp)
  851. eridx = np.array([-1, 3, 4, 0, 5, 1, -1, -1, 2], dtype=np.intp)
  852. assert isinstance(res, UInt64Index)
  853. tm.assert_index_equal(res, eres)
  854. tm.assert_numpy_array_equal(lidx, elidx)
  855. tm.assert_numpy_array_equal(ridx, eridx)
  856. # monotonic
  857. res, lidx, ridx = self.index.join(other_mono, how='outer',
  858. return_indexers=True)
  859. noidx_res = self.index.join(other_mono, how='outer')
  860. tm.assert_index_equal(res, noidx_res)
  861. elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp)
  862. eridx = np.array([-1, 0, 1, 2, 3, 4, -1, -1, 5], dtype=np.intp)
  863. assert isinstance(res, UInt64Index)
  864. tm.assert_index_equal(res, eres)
  865. tm.assert_numpy_array_equal(lidx, elidx)
  866. tm.assert_numpy_array_equal(ridx, eridx)