Skip to content

Commit 7106263

Browse files
committed
ENH: GH12034 RangeIndex.__floordiv__ returns RangeIndex if possible
1 parent 5b5b2fe commit 7106263

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Range Index
110110

111111
A ``RangeIndex`` has been added to the ``Int64Index`` sub-classes to support a memory saving alternative for common use cases. This has a similar implementation to the python ``range`` object (``xrange`` in python 2), in that it only stores the start, stop, and step values for the index. It will transparently interact with the user API, converting to ``Int64Index`` if needed.
112112

113-
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`)
113+
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`, :issue:`12070`)
114114

115115
Previous Behavior:
116116

pandas/core/index.py

+16
Original file line numberDiff line numberDiff line change
@@ -4360,6 +4360,22 @@ def __getitem__(self, key):
43604360
# fall back to Int64Index
43614361
return super_getitem(key)
43624362

4363+
def __floordiv__(self, other):
4364+
if com.is_integer(other):
4365+
if (len(self) == 0 or
4366+
self._start % other == 0 and
4367+
self._step % other == 0):
4368+
start = self._start // other
4369+
step = self._step // other
4370+
stop = start + len(self) * step
4371+
return RangeIndex(start, stop, step, name=self.name,
4372+
fastpath=True)
4373+
if len(self) == 1:
4374+
start = self._start // other
4375+
return RangeIndex(start, start + 1, 1, name=self.name,
4376+
fastpath=True)
4377+
return self._int64index // other
4378+
43634379
@classmethod
43644380
def _add_numeric_methods_binary(cls):
43654381
""" add in numeric methods, specialized to RangeIndex """

pandas/tests/test_index.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ def test_numeric_compat2(self):
37333733
self.assertTrue(result.equals(expected))
37343734

37353735
result = idx // 1
3736-
expected = idx._int64index // 1
3736+
expected = idx
37373737
tm.assert_index_equal(result, expected, exact=True)
37383738

37393739
# __mul__
@@ -3748,15 +3748,18 @@ def test_numeric_compat2(self):
37483748
tm.assert_index_equal(Index(result.values), expected, exact=True)
37493749

37503750
# __floordiv__
3751-
idx = RangeIndex(0, 1000, 2)
3752-
result = idx // 2
3753-
expected = idx._int64index // 2
3754-
tm.assert_index_equal(result, expected, exact=True)
3755-
3756-
idx = RangeIndex(0, 1000, 1)
3757-
result = idx // 2
3758-
expected = idx._int64index // 2
3759-
tm.assert_index_equal(result, expected, exact=True)
3751+
cases_exact = [(RangeIndex(0, 1000, 2), 2, RangeIndex(0, 500, 1)),
3752+
(RangeIndex(-99, -201, -3), -3, RangeIndex(33, 67, 1)),
3753+
(RangeIndex(0, 1000, 1), 2,
3754+
RangeIndex(0, 1000, 1)._int64index // 2),
3755+
(RangeIndex(0, 100, 1), 2.0,
3756+
RangeIndex(0, 100, 1)._int64index // 2.0),
3757+
(RangeIndex(), 50, RangeIndex()),
3758+
(RangeIndex(2, 4, 2), 3, RangeIndex(0, 1, 1)),
3759+
(RangeIndex(-5, -10, -6), 4, RangeIndex(-2, -1, 1)),
3760+
(RangeIndex(-100, -200, 3), 2, RangeIndex())]
3761+
for idx, div, expected in cases_exact:
3762+
tm.assert_index_equal(idx // div, expected, exact=True)
37603763

37613764
def test_constructor_corner(self):
37623765
arr = np.array([1, 2, 3, 4], dtype=object)
@@ -3857,7 +3860,6 @@ def test_is_monotonic(self):
38573860
self.assertTrue(index.is_monotonic_decreasing)
38583861

38593862
def test_equals(self):
3860-
38613863
equiv_pairs = [(RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),
38623864
(RangeIndex(0), RangeIndex(1, -1, 3)),
38633865
(RangeIndex(1, 2, 3), RangeIndex(1, 3, 4)),

0 commit comments

Comments
 (0)