Skip to content

Commit 90127a8

Browse files
authored
REF: RangeIndex tests (#33050)
1 parent e7ee418 commit 90127a8

File tree

2 files changed

+79
-69
lines changed

2 files changed

+79
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
from pandas import RangeIndex
6+
import pandas._testing as tm
7+
8+
9+
class TestGetIndexer:
10+
def test_get_indexer(self):
11+
index = RangeIndex(start=0, stop=20, step=2)
12+
target = RangeIndex(10)
13+
indexer = index.get_indexer(target)
14+
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
15+
tm.assert_numpy_array_equal(indexer, expected)
16+
17+
def test_get_indexer_pad(self):
18+
index = RangeIndex(start=0, stop=20, step=2)
19+
target = RangeIndex(10)
20+
indexer = index.get_indexer(target, method="pad")
21+
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
22+
tm.assert_numpy_array_equal(indexer, expected)
23+
24+
def test_get_indexer_backfill(self):
25+
index = RangeIndex(start=0, stop=20, step=2)
26+
target = RangeIndex(10)
27+
indexer = index.get_indexer(target, method="backfill")
28+
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
29+
tm.assert_numpy_array_equal(indexer, expected)
30+
31+
def test_get_indexer_limit(self):
32+
# GH#28631
33+
idx = RangeIndex(4)
34+
target = RangeIndex(6)
35+
result = idx.get_indexer(target, method="pad", limit=1)
36+
expected = np.array([0, 1, 2, 3, 3, -1], dtype=np.intp)
37+
tm.assert_numpy_array_equal(result, expected)
38+
39+
@pytest.mark.parametrize("stop", [0, -1, -2])
40+
def test_get_indexer_decreasing(self, stop):
41+
# GH#28678
42+
index = RangeIndex(7, stop, -3)
43+
result = index.get_indexer(range(9))
44+
expected = np.array([-1, 2, -1, -1, 1, -1, -1, 0, -1], dtype=np.intp)
45+
tm.assert_numpy_array_equal(result, expected)
46+
47+
48+
class TestTake:
49+
def test_take_preserve_name(self):
50+
index = RangeIndex(1, 5, name="foo")
51+
taken = index.take([3, 0, 1])
52+
assert index.name == taken.name
53+
54+
def test_take_fill_value(self):
55+
# GH#12631
56+
idx = pd.RangeIndex(1, 4, name="xxx")
57+
result = idx.take(np.array([1, 0, -1]))
58+
expected = pd.Int64Index([2, 1, 3], name="xxx")
59+
tm.assert_index_equal(result, expected)
60+
61+
# fill_value
62+
msg = "Unable to fill values because RangeIndex cannot contain NA"
63+
with pytest.raises(ValueError, match=msg):
64+
idx.take(np.array([1, 0, -1]), fill_value=True)
65+
66+
# allow_fill=False
67+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
68+
expected = pd.Int64Index([2, 1, 3], name="xxx")
69+
tm.assert_index_equal(result, expected)
70+
71+
msg = "Unable to fill values because RangeIndex cannot contain NA"
72+
with pytest.raises(ValueError, match=msg):
73+
idx.take(np.array([1, 0, -2]), fill_value=True)
74+
with pytest.raises(ValueError, match=msg):
75+
idx.take(np.array([1, 0, -5]), fill_value=True)
76+
77+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
78+
with pytest.raises(IndexError, match=msg):
79+
idx.take(np.array([1, -5]))

pandas/tests/indexes/ranges/test_range.py

-69
Original file line numberDiff line numberDiff line change
@@ -257,43 +257,6 @@ def test_identical(self):
257257

258258
assert not index.copy(dtype=object).identical(index.copy(dtype="int64"))
259259

260-
def test_get_indexer(self):
261-
index = self.create_index()
262-
target = RangeIndex(10)
263-
indexer = index.get_indexer(target)
264-
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
265-
tm.assert_numpy_array_equal(indexer, expected)
266-
267-
def test_get_indexer_pad(self):
268-
index = self.create_index()
269-
target = RangeIndex(10)
270-
indexer = index.get_indexer(target, method="pad")
271-
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
272-
tm.assert_numpy_array_equal(indexer, expected)
273-
274-
def test_get_indexer_backfill(self):
275-
index = self.create_index()
276-
target = RangeIndex(10)
277-
indexer = index.get_indexer(target, method="backfill")
278-
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
279-
tm.assert_numpy_array_equal(indexer, expected)
280-
281-
def test_get_indexer_limit(self):
282-
# GH 28631
283-
idx = RangeIndex(4)
284-
target = RangeIndex(6)
285-
result = idx.get_indexer(target, method="pad", limit=1)
286-
expected = np.array([0, 1, 2, 3, 3, -1], dtype=np.intp)
287-
tm.assert_numpy_array_equal(result, expected)
288-
289-
@pytest.mark.parametrize("stop", [0, -1, -2])
290-
def test_get_indexer_decreasing(self, stop):
291-
# GH 28678
292-
index = RangeIndex(7, stop, -3)
293-
result = index.get_indexer(range(9))
294-
expected = np.array([-1, 2, -1, -1, 1, -1, -1, 0, -1], dtype=np.intp)
295-
tm.assert_numpy_array_equal(result, expected)
296-
297260
def test_nbytes(self):
298261

299262
# memory savings vs int index
@@ -327,38 +290,6 @@ def test_prevent_casting(self):
327290
result = index.astype("O")
328291
assert result.dtype == np.object_
329292

330-
def test_take_preserve_name(self):
331-
index = RangeIndex(1, 5, name="foo")
332-
taken = index.take([3, 0, 1])
333-
assert index.name == taken.name
334-
335-
def test_take_fill_value(self):
336-
# GH 12631
337-
idx = pd.RangeIndex(1, 4, name="xxx")
338-
result = idx.take(np.array([1, 0, -1]))
339-
expected = pd.Int64Index([2, 1, 3], name="xxx")
340-
tm.assert_index_equal(result, expected)
341-
342-
# fill_value
343-
msg = "Unable to fill values because RangeIndex cannot contain NA"
344-
with pytest.raises(ValueError, match=msg):
345-
idx.take(np.array([1, 0, -1]), fill_value=True)
346-
347-
# allow_fill=False
348-
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
349-
expected = pd.Int64Index([2, 1, 3], name="xxx")
350-
tm.assert_index_equal(result, expected)
351-
352-
msg = "Unable to fill values because RangeIndex cannot contain NA"
353-
with pytest.raises(ValueError, match=msg):
354-
idx.take(np.array([1, 0, -2]), fill_value=True)
355-
with pytest.raises(ValueError, match=msg):
356-
idx.take(np.array([1, 0, -5]), fill_value=True)
357-
358-
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
359-
with pytest.raises(IndexError, match=msg):
360-
idx.take(np.array([1, -5]))
361-
362293
def test_repr_roundtrip(self):
363294
index = self.create_index()
364295
tm.assert_index_equal(eval(repr(index)), index)

0 commit comments

Comments
 (0)