Skip to content

Commit a1099a4

Browse files
committed
BUG: Fix Categorical comparsion with Series of dtype 'category'
This is a fix attempt for issue #16659.
1 parent 96a5274 commit a1099a4

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ PyPy
11681168
- Fix :func:`DataFrame.memory_usage` to support PyPy. Objects on PyPy do not have a fixed size,
11691169
so an approximation is used instead (:issue:`17228`)
11701170

1171+
11711172
Other
11721173
^^^^^
11731174
- Bug where some inplace operators were not being wrapped and produced a copy when invoked (:issue:`12962`)

pandas/core/categorical.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -2122,15 +2122,26 @@ def is_dtype_equal(self, other):
21222122
21232123
Parameters
21242124
----------
2125-
other : Categorical
2125+
other : Categorical, Series
21262126
21272127
Returns
21282128
-------
21292129
are_equal : boolean
21302130
"""
2131-
21322131
try:
2132+
<<<<<<< 965c1c89b6df471d88dc0e1188fb8cbc0d89f867
21332133
return hash(self.dtype) == hash(other.dtype)
2134+
=======
2135+
from pandas.core.series import Series
2136+
2137+
if isinstance(other, Series):
2138+
other_categorical = other.values
2139+
else:
2140+
other_categorical = other
2141+
2142+
return (self.categories.equals(other_categorical.categories) and
2143+
self.ordered == other_categorical.ordered)
2144+
>>>>>>> BUG: Fix Categorical comparsion with Series of dtype 'category'
21342145
except (AttributeError, TypeError):
21352146
return False
21362147

pandas/tests/test_categorical.py

+5
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ def test_is_equal_dtype(self):
202202
CategoricalIndex(c1, categories=list('cab'))))
203203
assert not c1.is_dtype_equal(CategoricalIndex(c1, ordered=True))
204204

205+
s1 = pd.Series(c1)
206+
assert c1.is_dtype_equal(s1)
207+
assert not c2.is_dtype_equal(s1)
208+
assert not c3.is_dtype_equal(s1)
209+
205210
def test_constructor(self):
206211

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

0 commit comments

Comments
 (0)