Skip to content

Commit c866a4a

Browse files
authored
BUG: Categorical.isin raising for overlapping intervals (#54951)
fix Categorical.isin raising for overlapping intervals
1 parent 1605bdf commit c866a4a

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Bug fixes
171171

172172
Categorical
173173
^^^^^^^^^^^
174-
-
174+
- :meth:`Categorical.isin` raising ``InvalidIndexError`` for categorical containing overlapping :class:`Interval` values (:issue:`34974`)
175175
-
176176

177177
Datetimelike

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
25972597
)
25982598
values = sanitize_array(values, None, None)
25992599
null_mask = np.asarray(isna(values))
2600-
code_values = self.categories.get_indexer(values)
2600+
code_values = self.categories.get_indexer_for(values)
26012601
code_values = code_values[null_mask | (code_values >= 0)]
26022602
return algorithms.isin(self.codes, code_values)
26032603

pandas/tests/indexes/categorical/test_category.py

+7
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ def test_isin(self):
228228
expected = np.array([False] * 5 + [True])
229229
tm.assert_numpy_array_equal(result, expected)
230230

231+
def test_isin_overlapping_intervals(self):
232+
# GH 34974
233+
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
234+
result = CategoricalIndex(idx).isin(idx)
235+
expected = np.array([True, True])
236+
tm.assert_numpy_array_equal(result, expected)
237+
231238
def test_identical(self):
232239
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
233240
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)

0 commit comments

Comments
 (0)