Skip to content

Commit 821aa25

Browse files
authored
BUG: Fix __ne__ comparison for Categorical (#32304)
1 parent ebf9668 commit 821aa25

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Categorical
199199

200200
- Bug where :func:`merge` was unable to join on non-unique categorical indices (:issue:`28189`)
201201
- Bug when passing categorical data to :class:`Index` constructor along with ``dtype=object`` incorrectly returning a :class:`CategoricalIndex` instead of object-dtype :class:`Index` (:issue:`32167`)
202+
- Bug where :class:`Categorical` comparison operator ``__ne__`` would incorrectly evaluate to ``False`` when either element was missing (:issue:`32276`)
202203
-
203204

204205
Datetimelike

pandas/core/arrays/categorical.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ def func(self, other):
103103
mask = (self._codes == -1) | (other_codes == -1)
104104
if mask.any():
105105
# In other series, the leads to False, so do that here too
106-
ret[mask] = False
106+
if opname == "__ne__":
107+
ret[(self._codes == -1) & (other_codes == -1)] = True
108+
else:
109+
ret[mask] = False
107110
return ret
108111

109112
if is_scalar(other):

pandas/tests/extension/test_categorical.py

+13
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,19 @@ def _compare_other(self, s, data, op_name, other):
282282
with pytest.raises(TypeError, match=msg):
283283
op(data, other)
284284

285+
@pytest.mark.parametrize(
286+
"categories",
287+
[["a", "b"], [0, 1], [pd.Timestamp("2019"), pd.Timestamp("2020")]],
288+
)
289+
def test_not_equal_with_na(self, categories):
290+
# https://github.com/pandas-dev/pandas/issues/32276
291+
c1 = Categorical.from_codes([-1, 0], categories=categories)
292+
c2 = Categorical.from_codes([0, 1], categories=categories)
293+
294+
result = c1 != c2
295+
296+
assert result.all()
297+
285298

286299
class TestParsing(base.BaseParsingTests):
287300
pass

0 commit comments

Comments
 (0)