Skip to content

Commit

Permalink
Fix index difference to follow the pandas format (rapidsai#14789)
Browse files Browse the repository at this point in the history
This PR fixes an error in `Index.difference` where the function keeps duplicate elements while pandas removes the duplicates. The tests had no inputs with duplicates, so I added new tests too (I added the test from the original issue). 

- closes rapidsai#14489

Authors:
  - AmirAli Mirian (https://github.com/amiralimi)
  - Ashwin Srinath (https://github.com/shwina)

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)

URL: rapidsai#14789
  • Loading branch information
amiralimi authored Jan 25, 2024
1 parent 5b1eef3 commit 0cd58fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/_base_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,11 @@ def difference(self, other, sort=None):
res_name = _get_result_name(self.name, other.name)

if is_mixed_with_object_dtype(self, other):
difference = self.copy()
difference = self.copy().unique()
else:
other = other.copy(deep=False)
difference = cudf.core.index._index_from_data(
cudf.DataFrame._from_data({"None": self._column})
cudf.DataFrame._from_data({"None": self._column.unique()})
.merge(
cudf.DataFrame._from_data({"None": other._column}),
how="leftanti",
Expand Down
4 changes: 3 additions & 1 deletion python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2023, NVIDIA CORPORATION.
# Copyright (c) 2018-2024, NVIDIA CORPORATION.

"""
Test related to Index
Expand Down Expand Up @@ -803,6 +803,7 @@ def test_index_to_series(data):
pd.Series(["1", "2", "a", "3", None], dtype="category"),
range(0, 10),
[],
[1, 1, 2, 2],
],
)
@pytest.mark.parametrize(
Expand All @@ -819,6 +820,7 @@ def test_index_to_series(data):
range(2, 4),
pd.Series(["1", "a", "3", None], dtype="category"),
[],
[2],
],
)
@pytest.mark.parametrize("sort", [None, False])
Expand Down

0 comments on commit 0cd58fb

Please sign in to comment.