Skip to content

Commit a581a74

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

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+4
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,12 @@ Numeric
631631

632632
Categorical
633633
^^^^^^^^^^^
634+
634635
- Bug in :func:`Series.isin` when called with a categorical (:issue`16639`)
635636
- Bug in the categorical constructor with empty values and categories causing the ``.categories`` to be an empty ``Float64Index`` rather than an empty ``Index`` with object dtype (:issue:`17248`)
636637
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>' not preserving the original Series' name (:issue:`17509`)
638+
- Bug in ``Categorical.is_dtype_equal()`` where comparison with a Series with dtype='category' (:issue:`16659`)
639+
637640

638641
PyPy
639642
^^^^
@@ -645,6 +648,7 @@ PyPy
645648
- Fix :func:`DataFrame.memory_usage` to support PyPy. Objects on PyPy do not have a fixed size,
646649
so an approximation is used instead (:issue:`17228`)
647650

651+
648652
Other
649653
^^^^^
650654
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)

pandas/core/categorical.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -2019,15 +2019,26 @@ def is_dtype_equal(self, other):
20192019
20202020
Parameters
20212021
----------
2022-
other : Categorical
2022+
other : Categorical, Series
20232023
20242024
Returns
20252025
-------
20262026
are_equal : boolean
20272027
"""
2028-
20292028
try:
2029+
<<<<<<< 965c1c89b6df471d88dc0e1188fb8cbc0d89f867
20302030
return hash(self.dtype) == hash(other.dtype)
2031+
=======
2032+
from pandas.core.series import Series
2033+
2034+
if isinstance(other, Series):
2035+
other_categorical = other.values
2036+
else:
2037+
other_categorical = other
2038+
2039+
return (self.categories.equals(other_categorical.categories) and
2040+
self.ordered == other_categorical.ordered)
2041+
>>>>>>> BUG: Fix Categorical comparsion with Series of dtype 'category'
20312042
except (AttributeError, TypeError):
20322043
return False
20332044

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)