Skip to content

BUG: Fix Categorical comparsion with Series of dtype 'category' #16667

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

Closed
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ PyPy
- Fix :func:`DataFrame.memory_usage` to support PyPy. Objects on PyPy do not have a fixed size,
so an approximation is used instead (:issue:`17228`)


Other
^^^^^
- Bug where some inplace operators were not being wrapped and produced a copy when invoked (:issue:`12962`)
Expand Down
18 changes: 16 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,15 +2122,29 @@ def is_dtype_equal(self, other):

Parameters
----------
other : Categorical
other : Categorical, Series

Returns
-------
are_equal : boolean
"""

try:
<<<<<<< 965c1c89b6df471d88dc0e1188fb8cbc0d89f867
return hash(self.dtype) == hash(other.dtype)
=======
from pandas.core.series import Series

if isinstance(other, Series):
other = Categorical(other)

<<<<<<< a581a743fe6740011e4fb0a7031ee92ce57b480b
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge issues

return (self.categories.equals(other_categorical.categories) and
self.ordered == other_categorical.ordered)
>>>>>>> BUG: Fix Categorical comparsion with Series of dtype 'category'
=======
return (self.categories.equals(other.categories) and
self.ordered == other.ordered)
>>>>>>> simplify the fix, add issue reference number to corresponding test and tighten the wording in doc whatsnew
except (AttributeError, TypeError):
return False

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

# GH 16659
s1 = pd.Series(c1)
assert c1.is_dtype_equal(s1)
assert not c2.is_dtype_equal(s1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u add some tests for things that are not is_dryoe_equal (i don't remember if these are sufficiently covered)

e.g. pass in scalers, ndarray, Dataframe

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and cycle thru all Indexes (except CI)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update for this

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