Skip to content

Commit 9106f09

Browse files
committed
BUG: Fixed Categorical.Equals with unordered
The original issue was already fixed. I added tests to verify (but no whatsnew entry). This addes tests and a fix for pandas-dev#16603 (comment) about `Categorical.equals` Closes pandas-dev#16603
1 parent dbec3c9 commit 9106f09

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

doc/source/whatsnew/v0.22.0.txt

-14
This file was deleted.

pandas/core/categorical.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -2081,8 +2081,16 @@ def equals(self, other):
20812081
-------
20822082
are_equal : boolean
20832083
"""
2084-
return (self.is_dtype_equal(other) and
2085-
np.array_equal(self._codes, other._codes))
2084+
if self.is_dtype_equal(other):
2085+
if self.categories.equals(other.categories):
2086+
# fastpath to avoid re-coding
2087+
return np.array_equal(self._codes, other._codes)
2088+
else:
2089+
# coerce their codes to ours
2090+
codes2 = _recode_for_categories(other.codes, other.categories,
2091+
self.categories)
2092+
return np.array_equal(self._codes, codes2)
2093+
return False
20862094

20872095
def is_dtype_equal(self, other):
20882096
"""

pandas/tests/categorical/test_operators.py

+7
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ def test_compare_different_lengths(self):
250250
with tm.assert_raises_regex(TypeError, msg):
251251
c1 == c2
252252

253+
def test_compare_unordered_different_order(self):
254+
# https://github.com/pandas-dev/pandas/issues/16603#issuecomment-
255+
# 349290078
256+
a = pd.Categorical(['a'], categories=['a', 'b'])
257+
b = pd.Categorical(['b'], categories=['b', 'a'])
258+
assert not a.equals(b)
259+
253260
def test_numeric_like_ops(self):
254261

255262
df = DataFrame({'value': np.random.randint(0, 10000, 100)})

pandas/tests/indexes/test_category.py

+9
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,15 @@ def test_equals_categorical(self):
747747
ordered=True))
748748
assert ci.equals(ci.copy())
749749

750+
def test_equals_categoridcal_unordered(self):
751+
# https://github.com/pandas-dev/pandas/issues/16603
752+
a = pd.CategoricalIndex(['A'], categories=['A', 'B'])
753+
b = pd.CategoricalIndex(['A'], categories=['B', 'A'])
754+
c = pd.CategoricalIndex(['C'], categories=['B', 'A'])
755+
assert a.equals(b)
756+
assert not a.equals(c)
757+
assert not b.equals(c)
758+
750759
def test_string_categorical_index_repr(self):
751760
# short
752761
idx = pd.CategoricalIndex(['a', 'bb', 'ccc'])

0 commit comments

Comments
 (0)