Skip to content

Commit 6a88d91

Browse files
committed
BUG: None comparison evaluates to True pandas-dev#26504
1 parent cf156cf commit 6a88d91

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Categorical
305305
^^^^^^^^^^^
306306

307307
- Bug in :func:`DataFrame.at` and :func:`Series.at` that would raise exception if the index was a :class:`CategoricalIndex` (:issue:`20629`)
308-
-
308+
- Bug in :func:`_cat_compare_op` that would valuate comparison with None to True (:issue:`26504`)
309309
-
310310

311311
Datetimelike

pandas/tests/arrays/categorical/test_operators.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,16 @@ def test_comparison_with_unknown_scalars(self):
188188

189189
def test_comparison_with_known_scalars(self):
190190
# https://github.com/pandas-dev/pandas/issues/26504
191-
# and following comparisons with scalars not in categories should raise
192-
# for unequal comps, but not for equal/not equal
193-
cat = Categorical([1, 2, 3, None], categories=[1, 2, 3], ordered=True)
191+
# and following comparisons with scalars in categories with None should
192+
# be evaluated as False
194193

195-
msg = ("Cannot compare a Categorical for op __{}__ with a scalar,"
196-
" which is not a category")
194+
cat1 = Categorical([1, 2, 3, None], categories=[1, 2, 3], ordered=True)
195+
cat2 = Categorical([None, 1, 2, 3], categories=[1, 2, 3], ordered=True)
197196

198-
tm.assert_numpy_array_equal(cat <= 2,
199-
np.array([True, True, False, False]))
197+
tm.assert_numpy_array_equal(cat1 <= 2,
198+
np.array([True, True, False, False]))
199+
tm.assert_numpy_array_equal(cat1 <= 2,
200+
np.array([False, True, True, False]))
200201

201202
@pytest.mark.parametrize('data,reverse,base', [
202203
(list("abc"), list("cba"), list("bbb")),

0 commit comments

Comments
 (0)