Skip to content

Commit de290f7

Browse files
authored
REF: use dispatch_reduction_ufunc in PandasArray.__array_ufunc__ (#44820)
1 parent 6331aca commit de290f7

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pandas/core/arrays/numpy_.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pandas.core.dtypes.missing import isna
1919

2020
from pandas.core import (
21+
arraylike,
2122
nanops,
2223
ops,
2324
)
@@ -144,6 +145,14 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
144145
if result is not NotImplemented:
145146
return result
146147

148+
if method == "reduce":
149+
result = arraylike.dispatch_reduction_ufunc(
150+
self, ufunc, method, *inputs, **kwargs
151+
)
152+
if result is not NotImplemented:
153+
# e.g. tests.series.test_ufunc.TestNumpyReductions
154+
return result
155+
147156
# Defer to the implementation of the ufunc on unwrapped values.
148157
inputs = tuple(x._ndarray if isinstance(x, PandasArray) else x for x in inputs)
149158
if out:
@@ -153,13 +162,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
153162
result = getattr(ufunc, method)(*inputs, **kwargs)
154163

155164
if ufunc.nout > 1:
156-
# multiple return values
157-
if not lib.is_scalar(result[0]):
158-
# re-box array-like results
159-
return tuple(type(self)(x) for x in result)
160-
else:
161-
# but not scalar reductions
162-
return result
165+
# multiple return values; re-box array-like results
166+
return tuple(type(self)(x) for x in result)
163167
elif method == "at":
164168
# no return value
165169
return None
@@ -171,11 +175,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
171175
# e.g. test_np_max_nested_tuples
172176
return result
173177
else:
174-
# one return value
175-
if not lib.is_scalar(result):
176-
# re-box array-like results, but not scalar reductions
177-
result = type(self)(result)
178-
return result
178+
# one return value; re-box array-like results
179+
return type(self)(result)
179180

180181
# ------------------------------------------------------------------------
181182
# Pandas ExtensionArray Interface

0 commit comments

Comments
 (0)