From a49ca2e88a64e4338aa3858e7696286bd3f27bfa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 8 Dec 2021 08:50:44 -0800 Subject: [PATCH] REF: use dispatch_reduction_ufunc in PandasArray.__array_ufunc__ --- pandas/core/arrays/numpy_.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index bfc537062a304..579d77369d27c 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -18,6 +18,7 @@ from pandas.core.dtypes.missing import isna from pandas.core import ( + arraylike, nanops, ops, ) @@ -144,6 +145,14 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): if result is not NotImplemented: return result + if method == "reduce": + result = arraylike.dispatch_reduction_ufunc( + self, ufunc, method, *inputs, **kwargs + ) + if result is not NotImplemented: + # e.g. tests.series.test_ufunc.TestNumpyReductions + return result + # Defer to the implementation of the ufunc on unwrapped values. inputs = tuple(x._ndarray if isinstance(x, PandasArray) else x for x in inputs) if out: @@ -153,13 +162,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): result = getattr(ufunc, method)(*inputs, **kwargs) if ufunc.nout > 1: - # multiple return values - if not lib.is_scalar(result[0]): - # re-box array-like results - return tuple(type(self)(x) for x in result) - else: - # but not scalar reductions - return result + # multiple return values; re-box array-like results + return tuple(type(self)(x) for x in result) elif method == "at": # no return value return None @@ -171,11 +175,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): # e.g. test_np_max_nested_tuples return result else: - # one return value - if not lib.is_scalar(result): - # re-box array-like results, but not scalar reductions - result = type(self)(result) - return result + # one return value; re-box array-like results + return type(self)(result) # ------------------------------------------------------------------------ # Pandas ExtensionArray Interface