@@ -220,13 +220,12 @@ def apply_empty_result(self):
220
220
221
221
def apply_raw (self ):
222
222
""" apply to the values as a numpy array """
223
- result , partial_result = libreduction .compute_reduction (
223
+ result , reduction_success = libreduction .compute_reduction (
224
224
self .values , self .f , axis = self .axis
225
225
)
226
226
227
- # A non None partial_result means that the reduction was unsuccessful
228
227
# We expect np.apply_along_axis to give a two-dimensional result, or raise.
229
- if partial_result is not None :
228
+ if not reduction_success :
230
229
result = np .apply_along_axis (self .f , self .axis , self .values )
231
230
232
231
# TODO: mixed type case
@@ -264,7 +263,8 @@ def apply_broadcast(self, target: "DataFrame") -> "DataFrame":
264
263
265
264
def apply_standard (self ):
266
265
267
- partial_result = None
266
+ partial_result = None # partial result that may be returned from reduction.
267
+
268
268
# try to reduce first (by default)
269
269
# this only matters if the reduction in values is of different dtype
270
270
# e.g. if we want to apply to a SparseFrame, then can't directly reduce
@@ -292,7 +292,7 @@ def apply_standard(self):
292
292
)
293
293
294
294
try :
295
- result , partial_result = libreduction .compute_reduction (
295
+ result , reduction_success = libreduction .compute_reduction (
296
296
values , self .f , axis = self .axis , dummy = dummy , labels = labels
297
297
)
298
298
except TypeError :
@@ -303,14 +303,17 @@ def apply_standard(self):
303
303
# reached via numexpr; fall back to python implementation
304
304
pass
305
305
else :
306
- # this means that the reduction was successful
307
- if partial_result is None :
306
+ if reduction_success :
308
307
return self .obj ._constructor_sliced (result , index = labels )
309
308
else :
309
+ # no exceptions - however reduction was unsuccessful,
310
+ # use the computed function result for first element
311
+ partial_result = result [0 ]
310
312
if isinstance (partial_result , ABCSeries ):
311
313
partial_result = partial_result .infer_objects ()
312
314
313
- # compute the result using the series generator
315
+ # compute the result using the series generator,
316
+ # use the result computed while trying to reduce if available.
314
317
results , res_index = self .apply_series_generator (partial_result )
315
318
316
319
# wrap results
0 commit comments