Skip to content

BUG: RangeIndex.difference with mismatched step #38028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ Other
- Bug in :meth:`Index.union` behaving differently depending on whether operand is an :class:`Index` or other list-like (:issue:`36384`)
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError`` rather than a bare ``Exception`` (:issue:`35744`)
- Bug in ``dir`` where ``dir(obj)`` wouldn't show attributes defined on the instance for pandas objects (:issue:`37173`)
- Bug in :meth:`RangeIndex.difference` returning :class:`Int64Index` in some cases where it should return :class:`RangeIndex` (:issue:`38028`)

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,13 +660,17 @@ def difference(self, other, sort=None):
if not isinstance(overlap, RangeIndex):
# We wont end up with RangeIndex, so fall back
return super().difference(other, sort=sort)
if overlap.step != first.step:
# In some cases we might be able to get a RangeIndex back,
# but not worth the effort.
return super().difference(other, sort=sort)

if overlap[0] == first.start:
# The difference is everything after the intersection
new_rng = range(overlap[-1] + first.step, first.stop, first.step)
elif overlap[-1] == first.stop:
elif overlap[-1] == first[-1]:
# The difference is everything before the intersection
new_rng = range(first.start, overlap[0] - first.step, first.step)
new_rng = range(first.start, overlap[0], first.step)
else:
# The difference is not range-like
return super().difference(other, sort=sort)
Expand Down
25 changes: 21 additions & 4 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,38 @@ def test_difference(self):

result = obj.difference(obj)
expected = RangeIndex.from_range(range(0), name="foo")
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact=True)

result = obj.difference(expected.rename("bar"))
tm.assert_index_equal(result, obj.rename(None))
tm.assert_index_equal(result, obj.rename(None), exact=True)

result = obj.difference(obj[:3])
tm.assert_index_equal(result, obj[3:])
tm.assert_index_equal(result, obj[3:], exact=True)

result = obj.difference(obj[-3:])
tm.assert_index_equal(result, obj[:-3])
tm.assert_index_equal(result, obj[:-3], exact=True)

result = obj[::-1].difference(obj[-3:])
tm.assert_index_equal(result, obj[:-3][::-1], exact=True)

result = obj[::-1].difference(obj[-3:][::-1])
tm.assert_index_equal(result, obj[:-3][::-1], exact=True)

result = obj.difference(obj[2:6])
expected = Int64Index([1, 2, 7, 8, 9], name="foo")
tm.assert_index_equal(result, expected)

def test_difference_mismatched_step(self):
obj = RangeIndex.from_range(range(1, 10), name="foo")

result = obj.difference(obj[::2])
expected = obj[1::2]._int64index
tm.assert_index_equal(result, expected, exact=True)

result = obj.difference(obj[1::2])
expected = obj[::2]._int64index
tm.assert_index_equal(result, expected, exact=True)

def test_symmetric_difference(self):
# GH#12034 Cases where we operate against another RangeIndex and may
# get back another RangeIndex
Expand Down