Skip to content

REF: avoid result=None case in _python_agg_general #29499

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 6 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 7 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ def _python_agg_general(self, func, *args, **kwargs):
# iterate through "columns" ex exclusions to populate output dict
output = {}
for name, obj in self._iterate_slices():
if self.grouper.ngroups == 0:
# agg_series below assumes ngroups > 0
# 3 test cases get here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: somewhat on the fence regarding the necessity and usefulness of this comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. its for my own reference in trying to move these checks earlier

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment on another PR but would ideally not special case things like this in groupby functions. Is there an actual issue in allowing this to follow the same code paths as when ngroups is non-zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd that goal is the reason for the "3 test cases" comment (which I'll remove). I want to identify the cases that get here and make this check unnecessary. But its much clearer to do this check and not end up with None on L914 and not be clear on how that happened.

continue

try:
# if this function is invalid for this dtype, we will ignore it.
func(obj[:0])
Expand All @@ -911,10 +916,8 @@ def _python_agg_general(self, func, *args, **kwargs):
pass

result, counts = self.grouper.agg_series(obj, f)
if result is not None:
# TODO: only 3 test cases get None here, do something
# in those cases
output[name] = self._try_cast(result, obj, numeric_only=True)
assert result is not None
output[name] = self._try_cast(result, obj, numeric_only=True)

if len(output) == 0:
return self._python_apply_general(f)
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ def _transform(
return result

def agg_series(self, obj: Series, func):
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0

if is_extension_array_dtype(obj.dtype) and obj.dtype.kind != "M":
# _aggregate_series_fast would raise TypeError when
# calling libreduction.Slider
Expand All @@ -608,8 +611,10 @@ def agg_series(self, obj: Series, func):
return self._aggregate_series_pure_python(obj, func)

def _aggregate_series_fast(self, obj, func):
# At this point we have already checked that obj.index is not a MultiIndex
# and that obj is backed by an ndarray, not ExtensionArray
# At this point we have already checked that
# - obj.index is not a MultiIndex
# - obj is backed by an ndarray, not ExtensionArray
# - ngroups != 0
func = self._is_builtin_func(func)

group_index, _, ngroups = self.group_info
Expand Down Expand Up @@ -797,6 +802,9 @@ def groupings(self):
]

def agg_series(self, obj: Series, func):
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0

if is_extension_array_dtype(obj.dtype):
# pre-empty SeriesBinGrouper from raising TypeError
# TODO: watch out, this can return None
Expand Down