Skip to content

BUG: Fixed Categorical.Equals with unordered #18822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Numeric
Categorical
^^^^^^^^^^^

-
- Bug in ``Categorical.equals`` between two unordered categories with the same categories, but in a different order (:issue:`16603`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use :func: here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't list all the Categorical methods in the API docs, and .equals is one of the excluded ones.

-
-

Expand Down
12 changes: 10 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,8 +2081,16 @@ def equals(self, other):
-------
are_equal : boolean
"""
return (self.is_dtype_equal(other) and
np.array_equal(self._codes, other._codes))
if self.is_dtype_equal(other):
if self.categories.equals(other.categories):
# fastpath to avoid re-coding
other_codes = other._codes
else:
other_codes = _recode_for_categories(other.codes,
other.categories,
self.categories)
return np.array_equal(self._codes, other_codes)
return False

def is_dtype_equal(self, other):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ def test_compare_different_lengths(self):
with tm.assert_raises_regex(TypeError, msg):
c1 == c2

def test_compare_unordered_different_order(self):
# https://github.com/pandas-dev/pandas/issues/16603#issuecomment-
# 349290078
a = pd.Categorical(['a'], categories=['a', 'b'])
b = pd.Categorical(['b'], categories=['b', 'a'])
assert not a.equals(b)

def test_numeric_like_ops(self):

df = DataFrame({'value': np.random.randint(0, 10000, 100)})
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,15 @@ def test_equals_categorical(self):
ordered=True))
assert ci.equals(ci.copy())

def test_equals_categoridcal_unordered(self):
# https://github.com/pandas-dev/pandas/issues/16603
a = pd.CategoricalIndex(['A'], categories=['A', 'B'])
b = pd.CategoricalIndex(['A'], categories=['B', 'A'])
c = pd.CategoricalIndex(['C'], categories=['B', 'A'])
assert a.equals(b)
assert not a.equals(c)
assert not b.equals(c)

def test_string_categorical_index_repr(self):
# short
idx = pd.CategoricalIndex(['a', 'bb', 'ccc'])
Expand Down