Skip to content

Commit

Permalink
BUG: Series.isin fails or categoricals (#16858)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviolov authored and jreback committed Jul 11, 2017
1 parent 6a85e88 commit d236f31
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Numeric

Categorical
^^^^^^^^^^^

- Bug in ``:func:Series.isin()`` when called with a categorical (:issue`16639`)


Other
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
# --------------- #
# dtype access #
# --------------- #

def _ensure_data(values, dtype=None):
"""
routine to ensure that our data is of the correct
Expand Down Expand Up @@ -113,7 +112,8 @@ def _ensure_data(values, dtype=None):

return values.asi8, dtype, 'int64'

elif is_categorical_dtype(values) or is_categorical_dtype(dtype):
elif (is_categorical_dtype(values) and
(is_categorical_dtype(dtype) or dtype is None)):
values = getattr(values, 'values', values)
values = values.codes
dtype = 'category'
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,16 @@ def test_large(self):
expected[1] = True
tm.assert_numpy_array_equal(result, expected)

def test_categorical_from_codes(self):
# GH 16639
vals = np.array([0, 1, 2, 0])
cats = ['a', 'b', 'c']
Sd = pd.Series(pd.Categorical(1).from_codes(vals, cats))
St = pd.Series(pd.Categorical(1).from_codes(np.array([0, 1]), cats))
expected = np.array([True, True, False, True])
result = algos.isin(Sd, St)
tm.assert_numpy_array_equal(expected, result)


class TestValueCounts(object):

Expand Down

0 comments on commit d236f31

Please sign in to comment.