Skip to content

REF: use _extract_result in Reducer.get_result #29702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ cdef class Reducer:
else:
res = self.f(chunk)

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


cdef inline _extract_result(object res):
cdef inline _extract_result(object res, bint squeeze=True):
""" extract the result object, it might be a 0-dim ndarray
or a len-1 0-dim, or a scalar """
if (not _is_sparse_array(res) and hasattr(res, 'values')
and util.is_array(res.values)):
res = res.values
if not np.isscalar(res):
if util.is_array(res):
if res.ndim == 0:
res = res.item()
elif res.ndim == 1 and len(res) == 1:
res = res[0]
if util.is_array(res):
if res.ndim == 0:
res = res.item()
elif squeeze and res.ndim == 1 and len(res) == 1:
res = res[0]
return res


Expand Down