diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index b84d468fff736..cb7b8a5987b96 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -277,7 +277,7 @@ def na_logical_op(x: np.ndarray, y, op): assert not (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)) x = ensure_object(x) y = ensure_object(y) - result = libops.vec_binop(x, y, op) + result = libops.vec_binop(x.ravel(), y.ravel(), op) else: # let null fall thru assert lib.is_scalar(y) @@ -298,7 +298,7 @@ def na_logical_op(x: np.ndarray, y, op): f"and scalar of type [{typ}]" ) - return result + return result.reshape(x.shape) def logical_op( diff --git a/pandas/tests/arithmetic/test_array_ops.py b/pandas/tests/arithmetic/test_array_ops.py new file mode 100644 index 0000000000000..d8aaa3183a1c6 --- /dev/null +++ b/pandas/tests/arithmetic/test_array_ops.py @@ -0,0 +1,21 @@ +import operator + +import numpy as np +import pytest + +import pandas._testing as tm +from pandas.core.ops.array_ops import na_logical_op + + +def test_na_logical_op_2d(): + left = np.arange(8).reshape(4, 2) + right = left.astype(object) + right[0, 0] = np.nan + + # Check that we fall back to the vec_binop branch + with pytest.raises(TypeError, match="unsupported operand type"): + operator.or_(left, right) + + result = na_logical_op(left, right, operator.or_) + expected = right + tm.assert_numpy_array_equal(result, expected)