Skip to content

REF: use idiomatic checks in __array_ufunc__ #44765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pandas/core/arraylike.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,16 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
reconstruct_kwargs = {}

def reconstruct(result):
if ufunc.nout > 1:
# np.modf, np.frexp, np.divmod
return tuple(_reconstruct(x) for x in result)

return _reconstruct(result)

def _reconstruct(result):
if lib.is_scalar(result):
return result

if isinstance(result, tuple):
# np.modf, np.frexp, np.divmod
return tuple(reconstruct(x) for x in result)

if result.ndim != self.ndim:
if method == "outer":
if self.ndim == 2:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ def reconstruct(x):
return x

result = getattr(ufunc, method)(*inputs2, **kwargs)
if isinstance(result, tuple):
if ufunc.nout > 1:
# e.g. np.divmod
return tuple(reconstruct(x) for x in result)
else:
return reconstruct(result)
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
)
result = getattr(ufunc, method)(*inputs, **kwargs)

if type(result) is tuple and len(result):
if ufunc.nout > 1:
# multiple return values
if not lib.is_scalar(result[0]):
# re-box array-like results
Expand All @@ -174,6 +174,13 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
elif method == "at":
# no return value
return None
elif method == "reduce":
if isinstance(result, np.ndarray):
# e.g. test_np_reduce_2d
return type(self)(result)

# e.g. test_np_max_nested_tuples
return result
else:
# one return value
if not lib.is_scalar(result):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
sp_values = getattr(ufunc, method)(self.sp_values, **kwargs)
fill_value = getattr(ufunc, method)(self.fill_value, **kwargs)

if isinstance(sp_values, tuple):
if ufunc.nout > 1:
# multiple outputs. e.g. modf
arrays = tuple(
self._simple_new(
Expand All @@ -1588,7 +1588,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
for sp_value, fv in zip(sp_values, fill_value)
)
return arrays
elif is_scalar(sp_values):
elif method == "reduce":
# e.g. reductions
return sp_values

Expand All @@ -1602,7 +1602,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
out = out[0]
return out

if type(result) is tuple:
if ufunc.nout > 1:
return tuple(type(self)(x) for x in result)
elif method == "at":
# no return value
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/arrays/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@ def test_validate_reduction_keyword_args():
arr.all(keepdims=True)


def test_np_max_nested_tuples():
# case where checking in ufunc.nout works while checking for tuples
# does not
vals = [
(("j", "k"), ("l", "m")),
(("l", "m"), ("o", "p")),
(("o", "p"), ("j", "k")),
]
ser = pd.Series(vals)
arr = ser.array

assert arr.max() is arr[2]
assert ser.max() is arr[2]

result = np.maximum.reduce(arr)
assert result == arr[2]

result = np.maximum.reduce(ser)
assert result == arr[2]


def test_np_reduce_2d():
raw = np.arange(12).reshape(4, 3)
arr = PandasArray(raw)

res = np.maximum.reduce(arr, axis=0)
tm.assert_extension_array_equal(res, arr[-1])

alt = arr.max(axis=0)
tm.assert_extension_array_equal(alt, arr[-1])


# ----------------------------------------------------------------------------
# Ops

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def reconstruct(x):
else:
return DecimalArray._from_sequence(x)

if isinstance(result, tuple):
if ufunc.nout > 1:
return tuple(reconstruct(x) for x in result)
else:
return reconstruct(result)
Expand Down