Skip to content

Commit 579b826

Browse files
authored
BUG: categorical dtype equality for level in different type (#55486)
* BUG: categorical dtype equality for level in different type * BUG: categorical dtype equality for level in different type - Comments#1
1 parent fe07fd5 commit 579b826

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ Bug fixes
285285
Categorical
286286
^^^^^^^^^^^
287287
- :meth:`Categorical.isin` raising ``InvalidIndexError`` for categorical containing overlapping :class:`Interval` values (:issue:`34974`)
288+
- Bug in :meth:`CategoricalDtype.__eq__` returning false for unordered categorical data with mixed types (:issue:`55468`)
288289
-
289290

290291
Datetimelike

pandas/core/dtypes/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def __eq__(self, other: object) -> bool:
456456

457457
# With object-dtype we need a comparison that identifies
458458
# e.g. int(2) as distinct from float(2)
459-
return hash(self) == hash(other)
459+
return set(left) == set(right)
460460

461461
def __repr__(self) -> str_type:
462462
if self.categories is None:

pandas/tests/dtypes/test_dtypes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,24 @@ def test_equal_but_different(self):
917917
assert c1 is not c2
918918
assert c1 != c2
919919

920+
def test_equal_but_different_mixed_dtypes(self):
921+
c1 = CategoricalDtype([1, 2, "3"])
922+
c2 = CategoricalDtype(["3", 1, 2])
923+
assert c1 is not c2
924+
assert c1 == c2
925+
926+
def test_equal_empty_ordered(self):
927+
c1 = CategoricalDtype([], ordered=True)
928+
c2 = CategoricalDtype([], ordered=True)
929+
assert c1 is not c2
930+
assert c1 == c2
931+
932+
def test_equal_empty_unordered(self):
933+
c1 = CategoricalDtype([])
934+
c2 = CategoricalDtype([])
935+
assert c1 is not c2
936+
assert c1 == c2
937+
920938
@pytest.mark.parametrize("v1, v2", [([1, 2, 3], [1, 2, 3]), ([1, 2, 3], [3, 2, 1])])
921939
def test_order_hashes_different(self, v1, v2):
922940
c1 = CategoricalDtype(v1, ordered=False)

0 commit comments

Comments
 (0)