diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 8c71681582063..015fdf1f45f47 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -192,7 +192,7 @@ Numeric Categorical ^^^^^^^^^^^ - +- Bug in ``:func:Series.isin()`` when called with a categorical (:issue`16639`) Other diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index d74c5e66ea1a9..b490bf787a037 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -38,7 +38,6 @@ # --------------- # # dtype access # # --------------- # - def _ensure_data(values, dtype=None): """ routine to ensure that our data is of the correct @@ -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' diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 063dcea5c76d6..9504d2a9426f0 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -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):