Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Series.isin fails or categoricals #16858

Merged
merged 4 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -188,7 +188,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