Skip to content

BUG: Categorical.isin raising for overlapping intervals #54951

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 1 commit into from
Sep 2, 2023
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Bug fixes

Categorical
^^^^^^^^^^^
-
- :meth:`Categorical.isin` raising ``InvalidIndexError`` for categorical containing overlapping :class:`Interval` values (:issue:`34974`)
-

Datetimelike
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,7 +2597,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
)
values = sanitize_array(values, None, None)
null_mask = np.asarray(isna(values))
code_values = self.categories.get_indexer(values)
code_values = self.categories.get_indexer_for(values)
code_values = code_values[null_mask | (code_values >= 0)]
return algorithms.isin(self.codes, code_values)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ def test_isin(self):
expected = np.array([False] * 5 + [True])
tm.assert_numpy_array_equal(result, expected)

def test_isin_overlapping_intervals(self):
# GH 34974
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
result = CategoricalIndex(idx).isin(idx)
expected = np.array([True, True])
tm.assert_numpy_array_equal(result, expected)

def test_identical(self):
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
Expand Down