Skip to content

Commit 35803bc

Browse files
committed
BUG: Fix NA comparison inconsistency for object dtype Series
Fix comp_method_OBJECT_ARRAY to return BooleanArray when input contains pd.NA values, ensuring NA is properly propagated in comparison results instead of returning False. Closes #63328
1 parent 944c527 commit 35803bc

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

pandas/core/ops/array_ops.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def fill_binop(left, right, fill_value):
113113

114114

115115
def comp_method_OBJECT_ARRAY(op, x, y):
116+
from pandas._libs import missing as libmissing
117+
from pandas.core.arrays import BooleanArray
118+
116119
if isinstance(y, list):
117120
# e.g. test_tuple_categories
118121
y = construct_1d_object_array_from_listlike(y)
@@ -129,7 +132,31 @@ def comp_method_OBJECT_ARRAY(op, x, y):
129132
result = libops.vec_compare(x.ravel(), y.ravel(), op)
130133
else:
131134
result = libops.scalar_compare(x.ravel(), y, op)
132-
return result.reshape(x.shape)
135+
result = result.reshape(x.shape)
136+
137+
# GH#63328: Check if there are pd.NA values in the input and return
138+
# BooleanArray to properly propagate NA in comparisons
139+
x_has_na = any(val is libmissing.NA for val in x.ravel())
140+
y_has_na = (
141+
is_scalar(y) and y is libmissing.NA
142+
) or (
143+
isinstance(y, np.ndarray)
144+
and any(val is libmissing.NA for val in y.ravel())
145+
)
146+
147+
if x_has_na or y_has_na:
148+
# Create a mask for NA values
149+
mask = np.array([val is libmissing.NA for val in x.ravel()], dtype=bool)
150+
if isinstance(y, np.ndarray):
151+
mask = mask | np.array(
152+
[val is libmissing.NA for val in y.ravel()], dtype=bool
153+
)
154+
elif y is libmissing.NA:
155+
mask = np.ones(x.shape, dtype=bool)
156+
mask = mask.reshape(x.shape)
157+
return BooleanArray(result, mask, copy=False)
158+
159+
return result
133160

134161

135162
def _masked_arith_op(x: np.ndarray, y, op) -> np.ndarray:

0 commit comments

Comments
 (0)