Skip to content

Commit d236f31

Browse files
aviolovjreback
authored andcommitted
BUG: Series.isin fails or categoricals (#16858)
1 parent 6a85e88 commit d236f31

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Numeric
192192

193193
Categorical
194194
^^^^^^^^^^^
195-
195+
- Bug in ``:func:Series.isin()`` when called with a categorical (:issue`16639`)
196196

197197

198198
Other

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
# --------------- #
3939
# dtype access #
4040
# --------------- #
41-
4241
def _ensure_data(values, dtype=None):
4342
"""
4443
routine to ensure that our data is of the correct
@@ -113,7 +112,8 @@ def _ensure_data(values, dtype=None):
113112

114113
return values.asi8, dtype, 'int64'
115114

116-
elif is_categorical_dtype(values) or is_categorical_dtype(dtype):
115+
elif (is_categorical_dtype(values) and
116+
(is_categorical_dtype(dtype) or dtype is None)):
117117
values = getattr(values, 'values', values)
118118
values = values.codes
119119
dtype = 'category'

pandas/tests/test_algos.py

+10
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,16 @@ def test_large(self):
586586
expected[1] = True
587587
tm.assert_numpy_array_equal(result, expected)
588588

589+
def test_categorical_from_codes(self):
590+
# GH 16639
591+
vals = np.array([0, 1, 2, 0])
592+
cats = ['a', 'b', 'c']
593+
Sd = pd.Series(pd.Categorical(1).from_codes(vals, cats))
594+
St = pd.Series(pd.Categorical(1).from_codes(np.array([0, 1]), cats))
595+
expected = np.array([True, True, False, True])
596+
result = algos.isin(Sd, St)
597+
tm.assert_numpy_array_equal(expected, result)
598+
589599

590600
class TestValueCounts(object):
591601

0 commit comments

Comments
 (0)