Skip to content

CLN: re-use invalid_comparison in Categorical comparisons #36229

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 1 commit into from
Sep 8, 2020
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
10 changes: 1 addition & 9 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,7 @@ def func(self, other):
ret[mask] = False
return ret
else:
if opname == "__eq__":
return np.zeros(len(self), dtype=bool)
elif opname == "__ne__":
return np.ones(len(self), dtype=bool)
else:
raise TypeError(
f"Cannot compare a Categorical for op {opname} with a "
"scalar, which is not a category."
)
return ops.invalid_comparison(self, other, op)
else:
# allow categorical vs object dtype array comparisons for equality
# these are only positional comparisons
Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,14 @@ def test_comparison_with_unknown_scalars(self):
# for unequal comps, but not for equal/not equal
cat = Categorical([1, 2, 3], ordered=True)

msg = (
"Cannot compare a Categorical for op __{}__ with a scalar, "
"which is not a category"
)
with pytest.raises(TypeError, match=msg.format("lt")):
msg = "Invalid comparison between dtype=category and int"
with pytest.raises(TypeError, match=msg):
cat < 4
with pytest.raises(TypeError, match=msg.format("gt")):
with pytest.raises(TypeError, match=msg):
cat > 4
with pytest.raises(TypeError, match=msg.format("gt")):
with pytest.raises(TypeError, match=msg):
4 < cat
with pytest.raises(TypeError, match=msg.format("lt")):
with pytest.raises(TypeError, match=msg):
4 > cat

tm.assert_numpy_array_equal(cat == 4, np.array([False, False, False]))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def test_unequal_categorical_comparison_raises_type_error(self):
# for unequal comps, but not for equal/not equal
cat = Series(Categorical(list("abc"), ordered=True))

msg = "Cannot compare a Categorical for op.+with a scalar"
msg = "Invalid comparison between dtype=category and str"
with pytest.raises(TypeError, match=msg):
cat < "d"
with pytest.raises(TypeError, match=msg):
Expand Down