Skip to content

Commit b5371e4

Browse files
jbrockmendeljreback
authored andcommitted
use _extract_result (#29702)
1 parent a39d9c8 commit b5371e4

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

pandas/_libs/reduction.pyx

+8-10
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ cdef class Reducer:
135135
else:
136136
res = self.f(chunk)
137137

138-
if (not _is_sparse_array(res) and hasattr(res, 'values')
139-
and util.is_array(res.values)):
140-
res = res.values
138+
# TODO: reason for not squeezing here?
139+
res = _extract_result(res, squeeze=False)
141140
if i == 0:
142141
# On the first pass, we check the output shape to see
143142
# if this looks like a reduction.
@@ -402,18 +401,17 @@ cdef class SeriesGrouper(_BaseGrouper):
402401
return result, counts
403402

404403

405-
cdef inline _extract_result(object res):
404+
cdef inline _extract_result(object res, bint squeeze=True):
406405
""" extract the result object, it might be a 0-dim ndarray
407406
or a len-1 0-dim, or a scalar """
408407
if (not _is_sparse_array(res) and hasattr(res, 'values')
409408
and util.is_array(res.values)):
410409
res = res.values
411-
if not np.isscalar(res):
412-
if util.is_array(res):
413-
if res.ndim == 0:
414-
res = res.item()
415-
elif res.ndim == 1 and len(res) == 1:
416-
res = res[0]
410+
if util.is_array(res):
411+
if res.ndim == 0:
412+
res = res.item()
413+
elif squeeze and res.ndim == 1 and len(res) == 1:
414+
res = res[0]
417415
return res
418416

419417

0 commit comments

Comments
 (0)