Skip to content

BUG: RangeIndex.difference with sort=None and step<0 #44085

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 2 commits into from
Oct 18, 2021
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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ Other
^^^^^
- Bug in :meth:`CustomBusinessMonthBegin.__add__` (:meth:`CustomBusinessMonthEnd.__add__`) not applying the extra ``offset`` parameter when beginning (end) of the target month is already a business day (:issue:`41356`)
- Bug in :meth:`RangeIndex.union` with another ``RangeIndex`` with matching (even) ``step`` and starts differing by strictly less than ``step / 2`` (:issue:`44019`)
- Bug in :meth:`RangeIndex.difference` with ``sort=None`` and ``step<0`` failing to sort (:issue:`44085`)

.. ***DO NOT USE THIS SECTION***

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@ def _difference(self, other, sort=None):
overlap = overlap[::-1]

if len(overlap) == 0:
return self.rename(name=res_name)
result = self.rename(name=res_name)
if sort is None and self.step < 0:
result = result[::-1]
return result
if len(overlap) == len(self):
return self[:0].rename(res_name)
if not isinstance(overlap, RangeIndex):
Expand All @@ -704,6 +707,9 @@ def _difference(self, other, sort=None):
new_index = type(self)._simple_new(new_rng, name=res_name)
if first is not self._range:
new_index = new_index[::-1]

if sort is None and new_index.step < 0:
new_index = new_index[::-1]
return new_index

def symmetric_difference(self, other, result_name: Hashable = None, sort=None):
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/indexes/ranges/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,44 @@ def test_difference(self):
result = obj.difference(obj[-3:])
tm.assert_index_equal(result, obj[:-3], exact=True)

# Flipping the step of 'other' doesn't affect the result, but
# flipping the stepof 'self' does when sort=None
result = obj[::-1].difference(obj[-3:])
tm.assert_index_equal(result, obj[:-3], exact=True)

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

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

result = obj[::-1].difference(obj[-3:][::-1], sort=False)
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_sort(self):
# GH#44085 ensure we respect the sort keyword

idx = Index(range(4))[::-1]
other = Index(range(3, 4))

result = idx.difference(other)
expected = Index(range(3))
tm.assert_index_equal(result, expected, exact=True)

result = idx.difference(other, sort=False)
expected = expected[::-1]
tm.assert_index_equal(result, expected, exact=True)

# case where the intersection is empty
other = range(10, 12)
result = idx.difference(other, sort=None)
expected = idx[::-1]
tm.assert_index_equal(result, expected, exact=True)

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

Expand Down