Skip to content

Commit d619a76

Browse files
committed
fixup! BUG: Categorical comparison with unordered
1 parent 9910de4 commit d619a76

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

pandas/core/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def f(self, other):
7070
if not (self.ordered == other.ordered):
7171
raise TypeError("Categoricals can only be compared if "
7272
"'ordered' is the same")
73-
if not self.ordered:
74-
# Comparison uses codes, so align theirs to ours
73+
if not self.categories.equals(other.categories):
74+
# both unordered and different order
7575
other_codes = _get_codes_for_values(other, self.categories)
7676
else:
7777
other_codes = other._codes

pandas/tests/test_categorical.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3848,16 +3848,16 @@ def test_unordered_different_order_equal(self, ctor):
38483848
def test_unordered_different_categories_raises(self):
38493849
c1 = Categorical(['a', 'b'], categories=['a', 'b'], ordered=False)
38503850
c2 = Categorical(['a', 'c'], categories=['c', 'a'], ordered=False)
3851-
with pytest.raises(TypeError) as rec:
3851+
with tm.assert_raises_regex(TypeError,
3852+
"Categoricals can only be compared"):
38523853
c1 == c2
3853-
assert rec.match("Categoricals can only be compared")
38543854

38553855
def test_compare_different_lengths(self):
38563856
c1 = Categorical([], categories=['a', 'b'])
38573857
c2 = Categorical([], categories=['a'])
3858-
with pytest.raises(TypeError) as rec:
3858+
msg = "Categories are different lengths"
3859+
with tm.assert_raises_regex(TypeError, msg):
38593860
c1 == c2
3860-
assert rec.match("Categories are different lengths")
38613861

38623862
def test_concat_append(self):
38633863
cat = pd.Categorical(["a", "b"], categories=["a", "b"])

0 commit comments

Comments
 (0)