18
18
from pandas .core .dtypes .missing import isna
19
19
20
20
from pandas .core import (
21
+ arraylike ,
21
22
nanops ,
22
23
ops ,
23
24
)
@@ -144,6 +145,14 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
144
145
if result is not NotImplemented :
145
146
return result
146
147
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
+
147
156
# Defer to the implementation of the ufunc on unwrapped values.
148
157
inputs = tuple (x ._ndarray if isinstance (x , PandasArray ) else x for x in inputs )
149
158
if out :
@@ -153,13 +162,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
153
162
result = getattr (ufunc , method )(* inputs , ** kwargs )
154
163
155
164
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 )
163
167
elif method == "at" :
164
168
# no return value
165
169
return None
@@ -171,11 +175,8 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
171
175
# e.g. test_np_max_nested_tuples
172
176
return result
173
177
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 )
179
180
180
181
# ------------------------------------------------------------------------
181
182
# Pandas ExtensionArray Interface
0 commit comments