Skip to content

Align cython and python reduction code paths #36459

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 4 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
REF: match libreduction pattern
  • Loading branch information
jbrockmendel committed Sep 18, 2020
commit 2702f899e7fb03348cb58d601188b26a802ca4d9
5 changes: 4 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@ def _aggregate_named(self, func, *args, **kwargs):
initialized = False

for name, group in self:
group.name = name
# Each step of this loop corresponds to
# libreduction._BaseGrouper._apply_to_group
group.name = name # NB: libreduction does not pin name

output = func(group, *args, **kwargs)
output = libreduction.extract_result(output)
if not initialized:
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,23 +653,26 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):
group_index, _, ngroups = self.group_info

counts = np.zeros(ngroups, dtype=int)
result = None
result = np.empty(ngroups, dtype="O")
initialized = False

splitter = get_splitter(obj, group_index, ngroups, axis=0)

for label, group in splitter:

# Each step of this loop corresponds to
# libreduction._BaseGrouper._apply_to_group
res = func(group)
res = libreduction.extract_result(res)

if result is None:
if not initialized:
# We only do this validation on the first iteration
libreduction.check_result_array(res, 0)
result = np.empty(ngroups, dtype="O")
initialized = True

counts[label] = group.shape[0]
result[label] = res

assert result is not None
result = lib.maybe_convert_objects(result, try_float=0)
# TODO: maybe_cast_to_extension_array?

Expand Down