Skip to content

Commit be2f558

Browse files
committed
Reduction returns success value and partial result is returned in results
1 parent 40646e2 commit be2f558

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

pandas/_libs/reduction.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ cdef class Reducer:
107107

108108
result = np.empty(self.nresults, dtype='O')
109109
it = <flatiter>PyArray_IterNew(result)
110-
partial_result = None
110+
reduction_success = True
111111

112112
try:
113113
for i in range(self.nresults):
@@ -150,7 +150,8 @@ cdef class Reducer:
150150
# catch only the specific exception
151151
raise
152152

153-
partial_result = copy(res)
153+
reduction_success = False
154+
PyArray_SETITEM(result, PyArray_ITER_DATA(it), copy(res))
154155
break
155156

156157
PyArray_SETITEM(result, PyArray_ITER_DATA(it), extracted_res)
@@ -162,7 +163,7 @@ cdef class Reducer:
162163
chunk.data = dummy_buf
163164

164165
result = maybe_convert_objects(result)
165-
return result, partial_result
166+
return result, reduction_success
166167

167168

168169
cdef class _BaseGrouper:

pandas/core/apply.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,12 @@ def apply_empty_result(self):
220220

221221
def apply_raw(self):
222222
""" apply to the values as a numpy array """
223-
result, partial_result = libreduction.compute_reduction(
223+
result, reduction_success = libreduction.compute_reduction(
224224
self.values, self.f, axis=self.axis
225225
)
226226

227-
# A non None partial_result means that the reduction was unsuccessful
228227
# 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:
230229
result = np.apply_along_axis(self.f, self.axis, self.values)
231230

232231
# TODO: mixed type case
@@ -264,7 +263,8 @@ def apply_broadcast(self, target: "DataFrame") -> "DataFrame":
264263

265264
def apply_standard(self):
266265

267-
partial_result = None
266+
partial_result = None # partial result that may be returned from reduction.
267+
268268
# try to reduce first (by default)
269269
# this only matters if the reduction in values is of different dtype
270270
# e.g. if we want to apply to a SparseFrame, then can't directly reduce
@@ -292,7 +292,7 @@ def apply_standard(self):
292292
)
293293

294294
try:
295-
result, partial_result = libreduction.compute_reduction(
295+
result, reduction_success = libreduction.compute_reduction(
296296
values, self.f, axis=self.axis, dummy=dummy, labels=labels
297297
)
298298
except TypeError:
@@ -303,14 +303,17 @@ def apply_standard(self):
303303
# reached via numexpr; fall back to python implementation
304304
pass
305305
else:
306-
# this means that the reduction was successful
307-
if partial_result is None:
306+
if reduction_success:
308307
return self.obj._constructor_sliced(result, index=labels)
309308
else:
309+
# no exceptions - however reduction was unsuccessful,
310+
# use the computed function result for first element
311+
partial_result = result[0]
310312
if isinstance(partial_result, ABCSeries):
311313
partial_result = partial_result.infer_objects()
312314

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.
314317
results, res_index = self.apply_series_generator(partial_result)
315318

316319
# wrap results

0 commit comments

Comments
 (0)