Skip to content

Commit

Permalink
Check for the presence of all values in MultiIndex.isin (#13879)
Browse files Browse the repository at this point in the history
Fixes: #13861 

This PR fixes an issue in `MulitIndex.isin` where, previously we calculated partial matches. With this PR we will only return `True` for complete matches.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #13879
  • Loading branch information
galipremsagar authored Aug 14, 2023
1 parent 049248d commit 8b72662
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
18 changes: 6 additions & 12 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,18 +730,12 @@ def isin(self, values, level=None):
values_idx = cudf.MultiIndex.from_tuples(
values, names=self.names
)

res = []
for name in self.names:
level_idx = self.get_level_values(name)
value_idx = values_idx.get_level_values(name)

existence = level_idx.isin(value_idx)
res.append(existence)

result = res[0]
for i in res[1:]:
result = result & i
self_df = self.to_frame(index=False).reset_index()
values_df = values_idx.to_frame(index=False)
idx = self_df.merge(values_df)._data["index"]
res = cudf.core.column.full(size=len(self), fill_value=False)
res[idx] = True
result = res.values
else:
level_series = self.get_level_values(level)
result = level_series.isin(values)
Expand Down
4 changes: 4 additions & 0 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,11 +2235,15 @@ def test_isin_index(data, values):
[[1, 2, 3, 10, 100], ["red", "blue", "green", "pink", "white"]],
names=("number", "color"),
),
pd.MultiIndex.from_product(
[[0, 1], ["red", "blue", "green"]], names=("number", "color")
),
],
)
@pytest.mark.parametrize(
"values,level,err",
[
([(1, "red"), (2, "blue"), (0, "green")], None, None),
(["red", "orange", "yellow"], "color", None),
(["red", "white", "yellow"], "color", None),
([0, 1, 2, 10, 11, 15], "number", None),
Expand Down

0 comments on commit 8b72662

Please sign in to comment.