test_range.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import numpy as np
  4. import pytest
  5. from pandas.compat import PY3, range, u
  6. import pandas as pd
  7. from pandas import Float64Index, Index, Int64Index, RangeIndex, Series
  8. import pandas.util.testing as tm
  9. from .test_numeric import Numeric
  10. class TestRangeIndex(Numeric):
  11. _holder = RangeIndex
  12. _compat_props = ['shape', 'ndim', 'size']
  13. def setup_method(self, method):
  14. self.indices = dict(index=RangeIndex(0, 20, 2, name='foo'),
  15. index_dec=RangeIndex(18, -1, -2, name='bar'))
  16. self.setup_indices()
  17. def create_index(self):
  18. return RangeIndex(5)
  19. def test_can_hold_identifiers(self):
  20. idx = self.create_index()
  21. key = idx[0]
  22. assert idx._can_hold_identifiers_and_holds_name(key) is False
  23. def test_too_many_names(self):
  24. with pytest.raises(ValueError, match="^Length"):
  25. self.index.names = ["roger", "harold"]
  26. def test_constructor(self):
  27. index = RangeIndex(5)
  28. expected = np.arange(5, dtype=np.int64)
  29. assert isinstance(index, RangeIndex)
  30. assert index._start == 0
  31. assert index._stop == 5
  32. assert index._step == 1
  33. assert index.name is None
  34. tm.assert_index_equal(Index(expected), index)
  35. index = RangeIndex(1, 5)
  36. expected = np.arange(1, 5, dtype=np.int64)
  37. assert isinstance(index, RangeIndex)
  38. assert index._start == 1
  39. tm.assert_index_equal(Index(expected), index)
  40. index = RangeIndex(1, 5, 2)
  41. expected = np.arange(1, 5, 2, dtype=np.int64)
  42. assert isinstance(index, RangeIndex)
  43. assert index._step == 2
  44. tm.assert_index_equal(Index(expected), index)
  45. for index in [RangeIndex(0), RangeIndex(start=0), RangeIndex(stop=0),
  46. RangeIndex(0, 0)]:
  47. expected = np.empty(0, dtype=np.int64)
  48. assert isinstance(index, RangeIndex)
  49. assert index._start == 0
  50. assert index._stop == 0
  51. assert index._step == 1
  52. tm.assert_index_equal(Index(expected), index)
  53. for index in [RangeIndex(0, name='Foo'),
  54. RangeIndex(start=0, name='Foo'),
  55. RangeIndex(stop=0, name='Foo'),
  56. RangeIndex(0, 0, name='Foo')]:
  57. assert isinstance(index, RangeIndex)
  58. assert index.name == 'Foo'
  59. # we don't allow on a bare Index
  60. with pytest.raises(TypeError):
  61. Index(0, 1000)
  62. def test_constructor_invalid_args(self):
  63. msg = "RangeIndex\\(\\.\\.\\.\\) must be called with integers"
  64. with pytest.raises(TypeError, match=msg):
  65. RangeIndex()
  66. with pytest.raises(TypeError, match=msg):
  67. RangeIndex(name='Foo')
  68. # invalid args
  69. for i in [Index(['a', 'b']), Series(['a', 'b']), np.array(['a', 'b']),
  70. [], 'foo', datetime(2000, 1, 1, 0, 0), np.arange(0, 10),
  71. np.array([1]), [1]]:
  72. with pytest.raises(TypeError):
  73. RangeIndex(i)
  74. def test_constructor_same(self):
  75. # pass thru w and w/o copy
  76. index = RangeIndex(1, 5, 2)
  77. result = RangeIndex(index, copy=False)
  78. assert result.identical(index)
  79. result = RangeIndex(index, copy=True)
  80. tm.assert_index_equal(result, index, exact=True)
  81. result = RangeIndex(index)
  82. tm.assert_index_equal(result, index, exact=True)
  83. with pytest.raises(TypeError):
  84. RangeIndex(index, dtype='float64')
  85. def test_constructor_range(self):
  86. with pytest.raises(TypeError):
  87. RangeIndex(range(1, 5, 2))
  88. result = RangeIndex.from_range(range(1, 5, 2))
  89. expected = RangeIndex(1, 5, 2)
  90. tm.assert_index_equal(result, expected, exact=True)
  91. result = RangeIndex.from_range(range(5, 6))
  92. expected = RangeIndex(5, 6, 1)
  93. tm.assert_index_equal(result, expected, exact=True)
  94. # an invalid range
  95. result = RangeIndex.from_range(range(5, 1))
  96. expected = RangeIndex(0, 0, 1)
  97. tm.assert_index_equal(result, expected, exact=True)
  98. result = RangeIndex.from_range(range(5))
  99. expected = RangeIndex(0, 5, 1)
  100. tm.assert_index_equal(result, expected, exact=True)
  101. result = Index(range(1, 5, 2))
  102. expected = RangeIndex(1, 5, 2)
  103. tm.assert_index_equal(result, expected, exact=True)
  104. with pytest.raises(TypeError):
  105. Index(range(1, 5, 2), dtype='float64')
  106. def test_constructor_name(self):
  107. # GH12288
  108. orig = RangeIndex(10)
  109. orig.name = 'original'
  110. copy = RangeIndex(orig)
  111. copy.name = 'copy'
  112. assert orig.name == 'original'
  113. assert copy.name == 'copy'
  114. new = Index(copy)
  115. assert new.name == 'copy'
  116. new.name = 'new'
  117. assert orig.name == 'original'
  118. assert copy.name == 'copy'
  119. assert new.name == 'new'
  120. def test_constructor_corner(self):
  121. arr = np.array([1, 2, 3, 4], dtype=object)
  122. index = RangeIndex(1, 5)
  123. assert index.values.dtype == np.int64
  124. tm.assert_index_equal(index, Index(arr))
  125. # non-int raise Exception
  126. with pytest.raises(TypeError):
  127. RangeIndex('1', '10', '1')
  128. with pytest.raises(TypeError):
  129. RangeIndex(1.1, 10.2, 1.3)
  130. # invalid passed type
  131. with pytest.raises(TypeError):
  132. RangeIndex(1, 5, dtype='float64')
  133. def test_copy(self):
  134. i = RangeIndex(5, name='Foo')
  135. i_copy = i.copy()
  136. assert i_copy is not i
  137. assert i_copy.identical(i)
  138. assert i_copy._start == 0
  139. assert i_copy._stop == 5
  140. assert i_copy._step == 1
  141. assert i_copy.name == 'Foo'
  142. def test_repr(self):
  143. i = RangeIndex(5, name='Foo')
  144. result = repr(i)
  145. if PY3:
  146. expected = "RangeIndex(start=0, stop=5, step=1, name='Foo')"
  147. else:
  148. expected = "RangeIndex(start=0, stop=5, step=1, name=u'Foo')"
  149. assert result == expected
  150. result = eval(result)
  151. tm.assert_index_equal(result, i, exact=True)
  152. i = RangeIndex(5, 0, -1)
  153. result = repr(i)
  154. expected = "RangeIndex(start=5, stop=0, step=-1)"
  155. assert result == expected
  156. result = eval(result)
  157. tm.assert_index_equal(result, i, exact=True)
  158. def test_insert(self):
  159. idx = RangeIndex(5, name='Foo')
  160. result = idx[1:4]
  161. # test 0th element
  162. tm.assert_index_equal(idx[0:4], result.insert(0, idx[0]))
  163. # GH 18295 (test missing)
  164. expected = Float64Index([0, np.nan, 1, 2, 3, 4])
  165. for na in (np.nan, pd.NaT, None):
  166. result = RangeIndex(5).insert(1, na)
  167. tm.assert_index_equal(result, expected)
  168. def test_delete(self):
  169. idx = RangeIndex(5, name='Foo')
  170. expected = idx[1:].astype(int)
  171. result = idx.delete(0)
  172. tm.assert_index_equal(result, expected)
  173. assert result.name == expected.name
  174. expected = idx[:-1].astype(int)
  175. result = idx.delete(-1)
  176. tm.assert_index_equal(result, expected)
  177. assert result.name == expected.name
  178. with pytest.raises((IndexError, ValueError)):
  179. # either depending on numpy version
  180. result = idx.delete(len(idx))
  181. def test_view(self):
  182. i = RangeIndex(0, name='Foo')
  183. i_view = i.view()
  184. assert i_view.name == 'Foo'
  185. i_view = i.view('i8')
  186. tm.assert_numpy_array_equal(i.values, i_view)
  187. i_view = i.view(RangeIndex)
  188. tm.assert_index_equal(i, i_view)
  189. def test_dtype(self):
  190. assert self.index.dtype == np.int64
  191. def test_is_monotonic(self):
  192. assert self.index.is_monotonic is True
  193. assert self.index.is_monotonic_increasing is True
  194. assert self.index.is_monotonic_decreasing is False
  195. assert self.index._is_strictly_monotonic_increasing is True
  196. assert self.index._is_strictly_monotonic_decreasing is False
  197. index = RangeIndex(4, 0, -1)
  198. assert index.is_monotonic is False
  199. assert index._is_strictly_monotonic_increasing is False
  200. assert index.is_monotonic_decreasing is True
  201. assert index._is_strictly_monotonic_decreasing is True
  202. index = RangeIndex(1, 2)
  203. assert index.is_monotonic is True
  204. assert index.is_monotonic_increasing is True
  205. assert index.is_monotonic_decreasing is True
  206. assert index._is_strictly_monotonic_increasing is True
  207. assert index._is_strictly_monotonic_decreasing is True
  208. index = RangeIndex(2, 1)
  209. assert index.is_monotonic is True
  210. assert index.is_monotonic_increasing is True
  211. assert index.is_monotonic_decreasing is True
  212. assert index._is_strictly_monotonic_increasing is True
  213. assert index._is_strictly_monotonic_decreasing is True
  214. index = RangeIndex(1, 1)
  215. assert index.is_monotonic is True
  216. assert index.is_monotonic_increasing is True
  217. assert index.is_monotonic_decreasing is True
  218. assert index._is_strictly_monotonic_increasing is True
  219. assert index._is_strictly_monotonic_decreasing is True
  220. def test_equals_range(self):
  221. equiv_pairs = [(RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),
  222. (RangeIndex(0), RangeIndex(1, -1, 3)),
  223. (RangeIndex(1, 2, 3), RangeIndex(1, 3, 4)),
  224. (RangeIndex(0, -9, -2), RangeIndex(0, -10, -2))]
  225. for left, right in equiv_pairs:
  226. assert left.equals(right)
  227. assert right.equals(left)
  228. def test_logical_compat(self):
  229. idx = self.create_index()
  230. assert idx.all() == idx.values.all()
  231. assert idx.any() == idx.values.any()
  232. def test_identical(self):
  233. i = Index(self.index.copy())
  234. assert i.identical(self.index)
  235. # we don't allow object dtype for RangeIndex
  236. if isinstance(self.index, RangeIndex):
  237. return
  238. same_values_different_type = Index(i, dtype=object)
  239. assert not i.identical(same_values_different_type)
  240. i = self.index.copy(dtype=object)
  241. i = i.rename('foo')
  242. same_values = Index(i, dtype=object)
  243. assert same_values.identical(self.index.copy(dtype=object))
  244. assert not i.identical(self.index)
  245. assert Index(same_values, name='foo', dtype=object).identical(i)
  246. assert not self.index.copy(dtype=object).identical(
  247. self.index.copy(dtype='int64'))
  248. def test_get_indexer(self):
  249. target = RangeIndex(10)
  250. indexer = self.index.get_indexer(target)
  251. expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
  252. tm.assert_numpy_array_equal(indexer, expected)
  253. def test_get_indexer_pad(self):
  254. target = RangeIndex(10)
  255. indexer = self.index.get_indexer(target, method='pad')
  256. expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
  257. tm.assert_numpy_array_equal(indexer, expected)
  258. def test_get_indexer_backfill(self):
  259. target = RangeIndex(10)
  260. indexer = self.index.get_indexer(target, method='backfill')
  261. expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
  262. tm.assert_numpy_array_equal(indexer, expected)
  263. def test_join_outer(self):
  264. # join with Int64Index
  265. other = Int64Index(np.arange(25, 14, -1))
  266. res, lidx, ridx = self.index.join(other, how='outer',
  267. return_indexers=True)
  268. noidx_res = self.index.join(other, how='outer')
  269. tm.assert_index_equal(res, noidx_res)
  270. eres = Int64Index([0, 2, 4, 6, 8, 10, 12, 14, 15, 16, 17, 18, 19, 20,
  271. 21, 22, 23, 24, 25])
  272. elidx = np.array([0, 1, 2, 3, 4, 5, 6, 7, -1, 8, -1, 9,
  273. -1, -1, -1, -1, -1, -1, -1], dtype=np.intp)
  274. eridx = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 10, 9, 8, 7, 6,
  275. 5, 4, 3, 2, 1, 0], dtype=np.intp)
  276. assert isinstance(res, Int64Index)
  277. assert not isinstance(res, RangeIndex)
  278. tm.assert_index_equal(res, eres)
  279. tm.assert_numpy_array_equal(lidx, elidx)
  280. tm.assert_numpy_array_equal(ridx, eridx)
  281. # join with RangeIndex
  282. other = RangeIndex(25, 14, -1)
  283. res, lidx, ridx = self.index.join(other, how='outer',
  284. return_indexers=True)
  285. noidx_res = self.index.join(other, how='outer')
  286. tm.assert_index_equal(res, noidx_res)
  287. assert isinstance(res, Int64Index)
  288. assert not isinstance(res, RangeIndex)
  289. tm.assert_index_equal(res, eres)
  290. tm.assert_numpy_array_equal(lidx, elidx)
  291. tm.assert_numpy_array_equal(ridx, eridx)
  292. def test_join_inner(self):
  293. # Join with non-RangeIndex
  294. other = Int64Index(np.arange(25, 14, -1))
  295. res, lidx, ridx = self.index.join(other, how='inner',
  296. return_indexers=True)
  297. # no guarantee of sortedness, so sort for comparison purposes
  298. ind = res.argsort()
  299. res = res.take(ind)
  300. lidx = lidx.take(ind)
  301. ridx = ridx.take(ind)
  302. eres = Int64Index([16, 18])
  303. elidx = np.array([8, 9], dtype=np.intp)
  304. eridx = np.array([9, 7], dtype=np.intp)
  305. assert isinstance(res, Int64Index)
  306. tm.assert_index_equal(res, eres)
  307. tm.assert_numpy_array_equal(lidx, elidx)
  308. tm.assert_numpy_array_equal(ridx, eridx)
  309. # Join two RangeIndex
  310. other = RangeIndex(25, 14, -1)
  311. res, lidx, ridx = self.index.join(other, how='inner',
  312. return_indexers=True)
  313. assert isinstance(res, RangeIndex)
  314. tm.assert_index_equal(res, eres)
  315. tm.assert_numpy_array_equal(lidx, elidx)
  316. tm.assert_numpy_array_equal(ridx, eridx)
  317. def test_join_left(self):
  318. # Join with Int64Index
  319. other = Int64Index(np.arange(25, 14, -1))
  320. res, lidx, ridx = self.index.join(other, how='left',
  321. return_indexers=True)
  322. eres = self.index
  323. eridx = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 9, 7], dtype=np.intp)
  324. assert isinstance(res, RangeIndex)
  325. tm.assert_index_equal(res, eres)
  326. assert lidx is None
  327. tm.assert_numpy_array_equal(ridx, eridx)
  328. # Join withRangeIndex
  329. other = Int64Index(np.arange(25, 14, -1))
  330. res, lidx, ridx = self.index.join(other, how='left',
  331. return_indexers=True)
  332. assert isinstance(res, RangeIndex)
  333. tm.assert_index_equal(res, eres)
  334. assert lidx is None
  335. tm.assert_numpy_array_equal(ridx, eridx)
  336. def test_join_right(self):
  337. # Join with Int64Index
  338. other = Int64Index(np.arange(25, 14, -1))
  339. res, lidx, ridx = self.index.join(other, how='right',
  340. return_indexers=True)
  341. eres = other
  342. elidx = np.array([-1, -1, -1, -1, -1, -1, -1, 9, -1, 8, -1],
  343. dtype=np.intp)
  344. assert isinstance(other, Int64Index)
  345. tm.assert_index_equal(res, eres)
  346. tm.assert_numpy_array_equal(lidx, elidx)
  347. assert ridx is None
  348. # Join withRangeIndex
  349. other = RangeIndex(25, 14, -1)
  350. res, lidx, ridx = self.index.join(other, how='right',
  351. return_indexers=True)
  352. eres = other
  353. assert isinstance(other, RangeIndex)
  354. tm.assert_index_equal(res, eres)
  355. tm.assert_numpy_array_equal(lidx, elidx)
  356. assert ridx is None
  357. def test_join_non_int_index(self):
  358. other = Index([3, 6, 7, 8, 10], dtype=object)
  359. outer = self.index.join(other, how='outer')
  360. outer2 = other.join(self.index, how='outer')
  361. expected = Index([0, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18])
  362. tm.assert_index_equal(outer, outer2)
  363. tm.assert_index_equal(outer, expected)
  364. inner = self.index.join(other, how='inner')
  365. inner2 = other.join(self.index, how='inner')
  366. expected = Index([6, 8, 10])
  367. tm.assert_index_equal(inner, inner2)
  368. tm.assert_index_equal(inner, expected)
  369. left = self.index.join(other, how='left')
  370. tm.assert_index_equal(left, self.index.astype(object))
  371. left2 = other.join(self.index, how='left')
  372. tm.assert_index_equal(left2, other)
  373. right = self.index.join(other, how='right')
  374. tm.assert_index_equal(right, other)
  375. right2 = other.join(self.index, how='right')
  376. tm.assert_index_equal(right2, self.index.astype(object))
  377. def test_join_non_unique(self):
  378. other = Index([4, 4, 3, 3])
  379. res, lidx, ridx = self.index.join(other, return_indexers=True)
  380. eres = Int64Index([0, 2, 4, 4, 6, 8, 10, 12, 14, 16, 18])
  381. elidx = np.array([0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.intp)
  382. eridx = np.array([-1, -1, 0, 1, -1, -1, -1, -1, -1, -1, -1],
  383. dtype=np.intp)
  384. tm.assert_index_equal(res, eres)
  385. tm.assert_numpy_array_equal(lidx, elidx)
  386. tm.assert_numpy_array_equal(ridx, eridx)
  387. def test_join_self(self):
  388. kinds = 'outer', 'inner', 'left', 'right'
  389. for kind in kinds:
  390. joined = self.index.join(self.index, how=kind)
  391. assert self.index is joined
  392. @pytest.mark.parametrize("sort", [None, False])
  393. def test_intersection(self, sort):
  394. # intersect with Int64Index
  395. other = Index(np.arange(1, 6))
  396. result = self.index.intersection(other, sort=sort)
  397. expected = Index(np.sort(np.intersect1d(self.index.values,
  398. other.values)))
  399. tm.assert_index_equal(result, expected)
  400. result = other.intersection(self.index, sort=sort)
  401. expected = Index(np.sort(np.asarray(np.intersect1d(self.index.values,
  402. other.values))))
  403. tm.assert_index_equal(result, expected)
  404. # intersect with increasing RangeIndex
  405. other = RangeIndex(1, 6)
  406. result = self.index.intersection(other, sort=sort)
  407. expected = Index(np.sort(np.intersect1d(self.index.values,
  408. other.values)))
  409. tm.assert_index_equal(result, expected)
  410. # intersect with decreasing RangeIndex
  411. other = RangeIndex(5, 0, -1)
  412. result = self.index.intersection(other, sort=sort)
  413. expected = Index(np.sort(np.intersect1d(self.index.values,
  414. other.values)))
  415. tm.assert_index_equal(result, expected)
  416. # reversed (GH 17296)
  417. result = other.intersection(self.index, sort=sort)
  418. tm.assert_index_equal(result, expected)
  419. # GH 17296: intersect two decreasing RangeIndexes
  420. first = RangeIndex(10, -2, -2)
  421. other = RangeIndex(5, -4, -1)
  422. expected = first.astype(int).intersection(other.astype(int), sort=sort)
  423. result = first.intersection(other, sort=sort).astype(int)
  424. tm.assert_index_equal(result, expected)
  425. # reversed
  426. result = other.intersection(first, sort=sort).astype(int)
  427. tm.assert_index_equal(result, expected)
  428. index = RangeIndex(5)
  429. # intersect of non-overlapping indices
  430. other = RangeIndex(5, 10, 1)
  431. result = index.intersection(other, sort=sort)
  432. expected = RangeIndex(0, 0, 1)
  433. tm.assert_index_equal(result, expected)
  434. other = RangeIndex(-1, -5, -1)
  435. result = index.intersection(other, sort=sort)
  436. expected = RangeIndex(0, 0, 1)
  437. tm.assert_index_equal(result, expected)
  438. # intersection of empty indices
  439. other = RangeIndex(0, 0, 1)
  440. result = index.intersection(other, sort=sort)
  441. expected = RangeIndex(0, 0, 1)
  442. tm.assert_index_equal(result, expected)
  443. result = other.intersection(index, sort=sort)
  444. tm.assert_index_equal(result, expected)
  445. # intersection of non-overlapping values based on start value and gcd
  446. index = RangeIndex(1, 10, 2)
  447. other = RangeIndex(0, 10, 4)
  448. result = index.intersection(other, sort=sort)
  449. expected = RangeIndex(0, 0, 1)
  450. tm.assert_index_equal(result, expected)
  451. def test_union_noncomparable(self):
  452. from datetime import datetime, timedelta
  453. # corner case, non-Int64Index
  454. now = datetime.now()
  455. other = Index([now + timedelta(i) for i in range(4)], dtype=object)
  456. result = self.index.union(other)
  457. expected = Index(np.concatenate((self.index, other)))
  458. tm.assert_index_equal(result, expected)
  459. result = other.union(self.index)
  460. expected = Index(np.concatenate((other, self.index)))
  461. tm.assert_index_equal(result, expected)
  462. def test_union(self):
  463. RI = RangeIndex
  464. I64 = Int64Index
  465. cases = [(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)),
  466. (RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1)),
  467. (RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1)),
  468. (RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)),
  469. (RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1)),
  470. (RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1)),
  471. (RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1)),
  472. (RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2)),
  473. (RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1)),
  474. (RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5)),
  475. (RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5)),
  476. (RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1)),
  477. (RI(0), RI(0), RI(0)),
  478. (RI(0, -10, -2), RI(0), RI(0, -10, -2)),
  479. (RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2)),
  480. (RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2)),
  481. (RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1)),
  482. (RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5)),
  483. (RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5)),
  484. (RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4])),
  485. (RI(0, 10, 1), I64([]), RI(0, 10, 1)),
  486. (RI(0), I64([1, 5, 6]), I64([1, 5, 6]))]
  487. for idx1, idx2, expected in cases:
  488. res1 = idx1.union(idx2)
  489. res2 = idx2.union(idx1)
  490. res3 = idx1._int64index.union(idx2)
  491. tm.assert_index_equal(res1, expected, exact=True)
  492. tm.assert_index_equal(res2, expected, exact=True)
  493. tm.assert_index_equal(res3, expected)
  494. def test_nbytes(self):
  495. # memory savings vs int index
  496. i = RangeIndex(0, 1000)
  497. assert i.nbytes < i._int64index.nbytes / 10
  498. # constant memory usage
  499. i2 = RangeIndex(0, 10)
  500. assert i.nbytes == i2.nbytes
  501. def test_cant_or_shouldnt_cast(self):
  502. # can't
  503. with pytest.raises(TypeError):
  504. RangeIndex('foo', 'bar', 'baz')
  505. # shouldn't
  506. with pytest.raises(TypeError):
  507. RangeIndex('0', '1', '2')
  508. def test_view_Index(self):
  509. self.index.view(Index)
  510. def test_prevent_casting(self):
  511. result = self.index.astype('O')
  512. assert result.dtype == np.object_
  513. def test_take_preserve_name(self):
  514. index = RangeIndex(1, 5, name='foo')
  515. taken = index.take([3, 0, 1])
  516. assert index.name == taken.name
  517. def test_take_fill_value(self):
  518. # GH 12631
  519. idx = pd.RangeIndex(1, 4, name='xxx')
  520. result = idx.take(np.array([1, 0, -1]))
  521. expected = pd.Int64Index([2, 1, 3], name='xxx')
  522. tm.assert_index_equal(result, expected)
  523. # fill_value
  524. msg = "Unable to fill values because RangeIndex cannot contain NA"
  525. with pytest.raises(ValueError, match=msg):
  526. idx.take(np.array([1, 0, -1]), fill_value=True)
  527. # allow_fill=False
  528. result = idx.take(np.array([1, 0, -1]), allow_fill=False,
  529. fill_value=True)
  530. expected = pd.Int64Index([2, 1, 3], name='xxx')
  531. tm.assert_index_equal(result, expected)
  532. msg = "Unable to fill values because RangeIndex cannot contain NA"
  533. with pytest.raises(ValueError, match=msg):
  534. idx.take(np.array([1, 0, -2]), fill_value=True)
  535. with pytest.raises(ValueError, match=msg):
  536. idx.take(np.array([1, 0, -5]), fill_value=True)
  537. with pytest.raises(IndexError):
  538. idx.take(np.array([1, -5]))
  539. def test_print_unicode_columns(self):
  540. df = pd.DataFrame({u("\u05d0"): [1, 2, 3],
  541. "\u05d1": [4, 5, 6],
  542. "c": [7, 8, 9]})
  543. repr(df.columns) # should not raise UnicodeDecodeError
  544. def test_repr_roundtrip(self):
  545. tm.assert_index_equal(eval(repr(self.index)), self.index)
  546. def test_slice_keep_name(self):
  547. idx = RangeIndex(1, 2, name='asdf')
  548. assert idx.name == idx[1:].name
  549. def test_explicit_conversions(self):
  550. # GH 8608
  551. # add/sub are overridden explicitly for Float/Int Index
  552. idx = RangeIndex(5)
  553. # float conversions
  554. arr = np.arange(5, dtype='int64') * 3.2
  555. expected = Float64Index(arr)
  556. fidx = idx * 3.2
  557. tm.assert_index_equal(fidx, expected)
  558. fidx = 3.2 * idx
  559. tm.assert_index_equal(fidx, expected)
  560. # interops with numpy arrays
  561. expected = Float64Index(arr)
  562. a = np.zeros(5, dtype='float64')
  563. result = fidx - a
  564. tm.assert_index_equal(result, expected)
  565. expected = Float64Index(-arr)
  566. a = np.zeros(5, dtype='float64')
  567. result = a - fidx
  568. tm.assert_index_equal(result, expected)
  569. def test_has_duplicates(self):
  570. for ind in self.indices:
  571. if not len(ind):
  572. continue
  573. idx = self.indices[ind]
  574. assert idx.is_unique
  575. assert not idx.has_duplicates
  576. def test_extended_gcd(self):
  577. result = self.index._extended_gcd(6, 10)
  578. assert result[0] == result[1] * 6 + result[2] * 10
  579. assert 2 == result[0]
  580. result = self.index._extended_gcd(10, 6)
  581. assert 2 == result[1] * 10 + result[2] * 6
  582. assert 2 == result[0]
  583. def test_min_fitting_element(self):
  584. result = RangeIndex(0, 20, 2)._min_fitting_element(1)
  585. assert 2 == result
  586. result = RangeIndex(1, 6)._min_fitting_element(1)
  587. assert 1 == result
  588. result = RangeIndex(18, -2, -2)._min_fitting_element(1)
  589. assert 2 == result
  590. result = RangeIndex(5, 0, -1)._min_fitting_element(1)
  591. assert 1 == result
  592. big_num = 500000000000000000000000
  593. result = RangeIndex(5, big_num * 2, 1)._min_fitting_element(big_num)
  594. assert big_num == result
  595. def test_max_fitting_element(self):
  596. result = RangeIndex(0, 20, 2)._max_fitting_element(17)
  597. assert 16 == result
  598. result = RangeIndex(1, 6)._max_fitting_element(4)
  599. assert 4 == result
  600. result = RangeIndex(18, -2, -2)._max_fitting_element(17)
  601. assert 16 == result
  602. result = RangeIndex(5, 0, -1)._max_fitting_element(4)
  603. assert 4 == result
  604. big_num = 500000000000000000000000
  605. result = RangeIndex(5, big_num * 2, 1)._max_fitting_element(big_num)
  606. assert big_num == result
  607. def test_pickle_compat_construction(self):
  608. # RangeIndex() is a valid constructor
  609. pass
  610. def test_slice_specialised(self):
  611. # scalar indexing
  612. res = self.index[1]
  613. expected = 2
  614. assert res == expected
  615. res = self.index[-1]
  616. expected = 18
  617. assert res == expected
  618. # slicing
  619. # slice value completion
  620. index = self.index[:]
  621. expected = self.index
  622. tm.assert_index_equal(index, expected)
  623. # positive slice values
  624. index = self.index[7:10:2]
  625. expected = Index(np.array([14, 18]), name='foo')
  626. tm.assert_index_equal(index, expected)
  627. # negative slice values
  628. index = self.index[-1:-5:-2]
  629. expected = Index(np.array([18, 14]), name='foo')
  630. tm.assert_index_equal(index, expected)
  631. # stop overshoot
  632. index = self.index[2:100:4]
  633. expected = Index(np.array([4, 12]), name='foo')
  634. tm.assert_index_equal(index, expected)
  635. # reverse
  636. index = self.index[::-1]
  637. expected = Index(self.index.values[::-1], name='foo')
  638. tm.assert_index_equal(index, expected)
  639. index = self.index[-8::-1]
  640. expected = Index(np.array([4, 2, 0]), name='foo')
  641. tm.assert_index_equal(index, expected)
  642. index = self.index[-40::-1]
  643. expected = Index(np.array([], dtype=np.int64), name='foo')
  644. tm.assert_index_equal(index, expected)
  645. index = self.index[40::-1]
  646. expected = Index(self.index.values[40::-1], name='foo')
  647. tm.assert_index_equal(index, expected)
  648. index = self.index[10::-1]
  649. expected = Index(self.index.values[::-1], name='foo')
  650. tm.assert_index_equal(index, expected)
  651. def test_len_specialised(self):
  652. # make sure that our len is the same as
  653. # np.arange calc
  654. for step in np.arange(1, 6, 1):
  655. arr = np.arange(0, 5, step)
  656. i = RangeIndex(0, 5, step)
  657. assert len(i) == len(arr)
  658. i = RangeIndex(5, 0, step)
  659. assert len(i) == 0
  660. for step in np.arange(-6, -1, 1):
  661. arr = np.arange(5, 0, step)
  662. i = RangeIndex(5, 0, step)
  663. assert len(i) == len(arr)
  664. i = RangeIndex(0, 5, step)
  665. assert len(i) == 0
  666. def test_append(self):
  667. # GH16212
  668. RI = RangeIndex
  669. I64 = Int64Index
  670. F64 = Float64Index
  671. OI = Index
  672. cases = [([RI(1, 12, 5)], RI(1, 12, 5)),
  673. ([RI(0, 6, 4)], RI(0, 6, 4)),
  674. ([RI(1, 3), RI(3, 7)], RI(1, 7)),
  675. ([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
  676. ([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
  677. ([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
  678. ([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
  679. ([RI(-4, -8), RI(3, -4)], RI(0, 0)),
  680. ([RI(-4, -8), RI(3, 5)], RI(3, 5)),
  681. ([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])),
  682. ([RI(-2,), RI(3, 5)], RI(3, 5)),
  683. ([RI(2,), RI(2)], I64([0, 1, 0, 1])),
  684. ([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)),
  685. ([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])),
  686. ([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)),
  687. ([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])),
  688. ([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])),
  689. ([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])),
  690. ([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14]))
  691. ]
  692. for indices, expected in cases:
  693. result = indices[0].append(indices[1:])
  694. tm.assert_index_equal(result, expected, exact=True)
  695. if len(indices) == 2:
  696. # Append single item rather than list
  697. result2 = indices[0].append(indices[1])
  698. tm.assert_index_equal(result2, expected, exact=True)