Skip to content

REF: raise more selectively in libreduction #41298

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 2 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
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: 12 additions & 6 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ from pandas._libs.lib import (
)


cpdef check_result_array(object obj):
cdef cnp.dtype _dtype_obj = np.dtype("object")

if (is_array(obj) or
(isinstance(obj, list) and len(obj) == 0) or
getattr(obj, 'shape', None) == (0,)):
raise ValueError('Must produce aggregated value')

cpdef check_result_array(object obj, object dtype):
# Our operation is supposed to be an aggregation/reduction. If
# it returns an ndarray, this likely means an invalid operation has
# been passed. See test_apply_without_aggregation, test_agg_must_agg
if is_array(obj):
if dtype != _dtype_obj:
# If it is object dtype, the function can be a reduction/aggregation
# and still return an ndarray e.g. test_agg_over_numpy_arrays
raise ValueError("Must produce aggregated value")


cdef class _BaseGrouper:
Expand Down Expand Up @@ -89,7 +95,7 @@ cdef class _BaseGrouper:
# On the first pass, we check the output shape to see
# if this looks like a reduction.
initialized = True
check_result_array(res)
check_result_array(res, cached_series.dtype)

return res, initialized

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def _aggregate_named(self, func, *args, **kwargs):
output = libreduction.extract_result(output)
if not initialized:
# We only do this validation on the first iteration
libreduction.check_result_array(output)
libreduction.check_result_array(output, group.dtype)
initialized = True
result[name] = output

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):

if not initialized:
# We only do this validation on the first iteration
libreduction.check_result_array(res)
libreduction.check_result_array(res, group.dtype)
initialized = True

counts[i] = group.shape[0]
Expand Down