Skip to content

Commit 46f2327

Browse files
committed
ndarrays
1 parent 0c69bd0 commit 46f2327

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pandas/_libs/missing.pyx

+19
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ cdef inline bint is_null_period(v):
289289
def _create_binary_propagating_op(name, divmod=False):
290290

291291
def method(self, other):
292+
print("binop", other, type(other))
292293
if (other is C_NA or isinstance(other, str)
293294
or isinstance(other, (numbers.Number, np.bool_, np.int64, np.int_))
294295
or isinstance(other, np.ndarray) and not other.shape):
@@ -297,6 +298,15 @@ def _create_binary_propagating_op(name, divmod=False):
297298
else:
298299
return NA
299300

301+
elif isinstance(other, np.ndarray):
302+
out = np.empty(other.shape, dtype=object)
303+
out[:] = NA
304+
305+
if divmod:
306+
return out, out.copy()
307+
else:
308+
return out
309+
300310
return NotImplemented
301311

302312
method.__name__ = name
@@ -484,6 +494,8 @@ class NAType(C_NAType):
484494
return type(other)(1)
485495
else:
486496
return NA
497+
elif isinstance(other, np.ndarray):
498+
return np.where(other == 0, other.dtype.type(1), NA)
487499

488500
return NotImplemented
489501

@@ -495,6 +507,8 @@ class NAType(C_NAType):
495507
return other
496508
else:
497509
return NA
510+
elif isinstance(other, np.ndarray):
511+
return np.where((other == 1) | (other == -1), other, NA)
498512

499513
return NotImplemented
500514

@@ -534,18 +548,23 @@ class NAType(C_NAType):
534548

535549
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
536550
types = self._HANDLED_TYPES + (NAType,)
551+
print('array_ufunc', 'inputs', inputs)
537552
for x in inputs:
538553
if not isinstance(x, types):
554+
print('defer', x)
539555
return NotImplemented
540556

541557
if method != "__call__":
542558
raise ValueError(f"ufunc method '{method}' not supported for NA")
543559
result = maybe_dispatch_ufunc_to_dunder_op(self, ufunc, method, *inputs, **kwargs)
560+
print("dispatch result", result)
544561
if result is NotImplemented:
562+
# TODO: this is wrong for binary, ternary ufuncs. Should handle shape stuff.
545563
if ufunc.nout == 1:
546564
result = NA
547565
else:
548566
result = (NA,) * ufunc.nout
567+
549568
return result
550569

551570

pandas/tests/scalar/test_na_scalar.py

+22
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ def test_logical_not():
156156
assert ~NA is NA
157157

158158

159+
@pytest.mark.parametrize(
160+
"shape",
161+
[
162+
# (), # TODO: this fails, since we return a scalar. What to do?
163+
(3,),
164+
(3, 3),
165+
(1, 2, 3),
166+
],
167+
)
168+
def test_arithmetic_ndarray(shape, all_arithmetic_functions):
169+
op = all_arithmetic_functions
170+
a = np.zeros(shape)
171+
if op.__name__ == "pow":
172+
a += 5
173+
result = op(pd.NA, a)
174+
expected = np.full(a.shape, pd.NA, dtype=object)
175+
tm.assert_numpy_array_equal(result, expected)
176+
177+
178+
# TODO: test pow special with ndarray
179+
180+
159181
def test_is_scalar():
160182
assert is_scalar(NA) is True
161183

0 commit comments

Comments
 (0)