Skip to content

Commit

Permalink
BUG: categorical dtype equality for level in different type (#55486)
Browse files Browse the repository at this point in the history
* BUG: categorical dtype equality for level in different type

* BUG: categorical dtype equality for level in different type - Comments#1
  • Loading branch information
gupta-paras authored Oct 13, 2023
1 parent fe07fd5 commit 579b826
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Bug fixes
Categorical
^^^^^^^^^^^
- :meth:`Categorical.isin` raising ``InvalidIndexError`` for categorical containing overlapping :class:`Interval` values (:issue:`34974`)
- Bug in :meth:`CategoricalDtype.__eq__` returning false for unordered categorical data with mixed types (:issue:`55468`)
-

Datetimelike
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def __eq__(self, other: object) -> bool:

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

def __repr__(self) -> str_type:
if self.categories is None:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,24 @@ def test_equal_but_different(self):
assert c1 is not c2
assert c1 != c2

def test_equal_but_different_mixed_dtypes(self):
c1 = CategoricalDtype([1, 2, "3"])
c2 = CategoricalDtype(["3", 1, 2])
assert c1 is not c2
assert c1 == c2

def test_equal_empty_ordered(self):
c1 = CategoricalDtype([], ordered=True)
c2 = CategoricalDtype([], ordered=True)
assert c1 is not c2
assert c1 == c2

def test_equal_empty_unordered(self):
c1 = CategoricalDtype([])
c2 = CategoricalDtype([])
assert c1 is not c2
assert c1 == c2

@pytest.mark.parametrize("v1, v2", [([1, 2, 3], [1, 2, 3]), ([1, 2, 3], [3, 2, 1])])
def test_order_hashes_different(self, v1, v2):
c1 = CategoricalDtype(v1, ordered=False)
Expand Down

0 comments on commit 579b826

Please sign in to comment.