Skip to content

Commit

Permalink
BUG: Fix Categorical comparsion with Series of dtype 'category'
Browse files Browse the repository at this point in the history
This is a fix attempt for issue pandas-dev#16659.
  • Loading branch information
funnycrab committed Jul 22, 2017
1 parent 28622c5 commit 556f9c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ Categorical
^^^^^^^^^^^
- Bug in :func:`Series.isin` when called with a categorical (:issue`16639`)

- Bug in ``Categorical.is_dtype_equal()`` where comparison with Series whose dtype is 'category' is not handled correctly (:issue:`16659`)


Other
^^^^^
Expand Down
14 changes: 10 additions & 4 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,16 +1965,22 @@ def is_dtype_equal(self, other):
Parameters
----------
other : Categorical
other : Categorical, Series
Returns
-------
are_equal : boolean
"""

try:
return (self.categories.equals(other.categories) and
self.ordered == other.ordered)
from pandas.core.series import Series

if isinstance(other, Series):
other_categorical = other.values
else:
other_categorical = other

return (self.categories.equals(other_categorical.categories) and
self.ordered == other_categorical.ordered)
except (AttributeError, TypeError):
return False

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def test_is_equal_dtype(self):
CategoricalIndex(c1, categories=list('cab'))))
assert not c1.is_dtype_equal(CategoricalIndex(c1, ordered=True))

s1 = pd.Series(c1)
assert c1.is_dtype_equal(s1)
assert not c2.is_dtype_equal(s1)
assert not c3.is_dtype_equal(s1)

def test_constructor(self):

exp_arr = np.array(["a", "b", "c", "a", "b", "c"], dtype=np.object_)
Expand Down

0 comments on commit 556f9c3

Please sign in to comment.