Skip to content
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

Fix Index.difference to handle duplicate values when one of the inputs is empty #15016

Merged
merged 2 commits into from
Feb 10, 2024
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
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 @@ -1096,12 +1096,12 @@ def difference(self, other, sort=None):
other = cudf.Index(other, name=getattr(other, "name", self.name))

if not len(other):
res = self._get_reconciled_name_object(other)
res = self._get_reconciled_name_object(other).unique()
if sort:
return res.sort_values()
return res
elif self.equals(other):
res = self[:0]._get_reconciled_name_object(other)
res = self[:0]._get_reconciled_name_object(other).unique()
if sort:
return res.sort_values()
return res
Expand Down
21 changes: 19 additions & 2 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import cudf
from cudf.api.extensions import no_default
from cudf.api.types import is_bool_dtype
from cudf.core._compat import PANDAS_GE_200
from cudf.core._compat import PANDAS_GE_200, PANDAS_GE_220
from cudf.core.index import (
CategoricalIndex,
DatetimeIndex,
Expand Down Expand Up @@ -797,9 +797,26 @@ def test_index_to_series(data):
"name_data,name_other",
[("abc", "c"), (None, "abc"), ("abc", pd.NA), ("abc", "abc")],
)
def test_index_difference(data, other, sort, name_data, name_other):
def test_index_difference(request, data, other, sort, name_data, name_other):
pd_data = pd.Index(data, name=name_data)
pd_other = pd.Index(other, name=name_other)
request.applymarker(
pytest.mark.xfail(
condition=PANDAS_GE_220
and isinstance(pd_data.dtype, pd.CategoricalDtype)
and not isinstance(pd_other.dtype, pd.CategoricalDtype)
and pd_other.isnull().any(),
reason="https://github.com/pandas-dev/pandas/issues/57318",
)
)
request.applymarker(
pytest.mark.xfail(
condition=not PANDAS_GE_220
and len(pd_other) == 0
and len(pd_data) != len(pd_data.unique()),
reason="Bug fixed in pandas-2.2+",
)
)

gd_data = cudf.from_pandas(pd_data)
gd_other = cudf.from_pandas(pd_other)
Expand Down
Loading