Skip to content

Commit

Permalink
BUG: Categorical.isin raising for overlapping intervals (pandas-dev#5…
Browse files Browse the repository at this point in the history
…4951)

fix Categorical.isin raising for overlapping intervals
  • Loading branch information
lukemanley authored Sep 2, 2023
1 parent 1605bdf commit c866a4a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
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

0 comments on commit c866a4a

Please sign in to comment.