Skip to content

Commit dcfdd8e

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
CLN: simplify Categorical comparisons (pandas-dev#36250)
1 parent 557fa62 commit dcfdd8e

File tree

3 files changed

+3
-24
lines changed

3 files changed

+3
-24
lines changed

pandas/core/arrays/categorical.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,9 @@ def func(self, other):
7676
# the same (maybe up to ordering, depending on ordered)
7777

7878
msg = "Categoricals can only be compared if 'categories' are the same."
79-
if len(self.categories) != len(other.categories):
80-
raise TypeError(msg + " Categories are different lengths")
81-
elif self.ordered and not (self.categories == other.categories).all():
82-
raise TypeError(msg)
83-
elif not set(self.categories) == set(other.categories):
79+
if not self.is_dtype_equal(other):
8480
raise TypeError(msg)
8581

86-
if not (self.ordered == other.ordered):
87-
raise TypeError(
88-
"Categoricals can only be compared if 'ordered' is the same"
89-
)
9082
if not self.ordered and not self.categories.equals(other.categories):
9183
# both unordered and different order
9284
other_codes = _get_codes_for_values(other, self.categories)

pandas/tests/arrays/categorical/test_operators.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,13 @@ def test_comparisons(self):
7979

8080
cat_rev_base2 = Categorical(["b", "b", "b"], categories=["c", "b", "a", "d"])
8181

82-
msg = (
83-
"Categoricals can only be compared if 'categories' are the same. "
84-
"Categories are different lengths"
85-
)
8682
with pytest.raises(TypeError, match=msg):
8783
cat_rev > cat_rev_base2
8884

8985
# Only categories with same ordering information can be compared
9086
cat_unorderd = cat.set_ordered(False)
9187
assert not (cat > cat).any()
9288

93-
msg = "Categoricals can only be compared if 'ordered' is the same"
9489
with pytest.raises(TypeError, match=msg):
9590
cat > cat_unorderd
9691

@@ -321,7 +316,7 @@ def test_compare_different_lengths(self):
321316
c1 = Categorical([], categories=["a", "b"])
322317
c2 = Categorical([], categories=["a"])
323318

324-
msg = "Categories are different lengths"
319+
msg = "Categoricals can only be compared if 'categories' are the same."
325320
with pytest.raises(TypeError, match=msg):
326321
c1 == c2
327322

pandas/tests/indexes/categorical/test_category.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,7 @@ def test_equals_categorical(self):
402402
with pytest.raises(ValueError, match="Lengths must match"):
403403
ci1 == Index(["a", "b", "c"])
404404

405-
msg = (
406-
"categorical index comparisons must have the same categories "
407-
"and ordered attributes"
408-
"|"
409-
"Categoricals can only be compared if 'categories' are the same. "
410-
"Categories are different lengths"
411-
"|"
412-
"Categoricals can only be compared if 'ordered' is the same"
413-
)
405+
msg = "Categoricals can only be compared if 'categories' are the same"
414406
with pytest.raises(TypeError, match=msg):
415407
ci1 == ci2
416408
with pytest.raises(TypeError, match=msg):

0 commit comments

Comments
 (0)