Skip to content

Commit e0c5b87

Browse files
authored
BUG: Index.difference not always returning a unique set of values (#55113)
* BUG: Index.difference not always returning a unique set of values * whatsnew * udpate test * update test
1 parent abbb86e commit e0c5b87

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Interval
262262

263263
Indexing
264264
^^^^^^^^
265-
-
265+
- Bug in :meth:`Index.difference` not returning a unique set of values when ``other`` is empty or ``other`` is considered non-comparable (:issue:`55113`)
266266
-
267267

268268
Missing

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,14 +3598,14 @@ def difference(self, other, sort=None):
35983598

35993599
if len(other) == 0:
36003600
# Note: we do not (yet) sort even if sort=None GH#24959
3601-
result = self.rename(result_name)
3601+
result = self.unique().rename(result_name)
36023602
if sort is True:
36033603
return result.sort_values()
36043604
return result
36053605

36063606
if not self._should_compare(other):
36073607
# Nothing matches -> difference is everything
3608-
result = self.rename(result_name)
3608+
result = self.unique().rename(result_name)
36093609
if sort is True:
36103610
return result.sort_values()
36113611
return result

pandas/tests/indexes/test_setops.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,21 @@ def test_difference_name_preservation(self, index, second_name, expected, sort):
796796
assert result.name == expected
797797

798798
def test_difference_empty_arg(self, index, sort):
799-
first = index[5:20]
799+
first = index.copy()
800+
first = first[5:20]
800801
first.name = "name"
801802
result = first.difference([], sort)
803+
expected = index[5:20].unique()
804+
expected.name = "name"
805+
tm.assert_index_equal(result, expected)
802806

803-
tm.assert_index_equal(result, first)
807+
def test_difference_should_not_compare(self):
808+
# GH 55113
809+
left = Index([1, 1])
810+
right = Index([True])
811+
result = left.difference(right)
812+
expected = Index([1])
813+
tm.assert_index_equal(result, expected)
804814

805815
@pytest.mark.parametrize("index", ["string"], indirect=True)
806816
def test_difference_identity(self, index, sort):

0 commit comments

Comments
 (0)