Skip to content

Performance regression in stat_ops.FrameMultiIndexOps.time_op #35186

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

Closed
Closed
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
22 changes: 12 additions & 10 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,16 +1434,18 @@ def std(self, ddof: int = 1):
Series or DataFrame
Standard deviation of values within each group.
"""
return self._get_cythonized_result(
"group_var_float64",
aggregate=True,
needs_counts=True,
needs_values=True,
needs_2d=True,
cython_dtype=np.dtype(np.float64),
post_processing=lambda vals, inference: np.sqrt(vals),
ddof=ddof,
)
result = self.var(ddof=ddof)
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm you are reverting.

instead I would like to see if the generic function can be patched (e.g. something is fundamentally different).

cc @rhshadrach

Copy link
Member

Choose a reason for hiding this comment

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

I don't see a way. _get_cythonized_result operates column-by-column, creating overhead. On data this size, we spend 25% in grouper.group_info and 40% in _wrap_aggregated_output. Within each of these operations, I don't see any easy wins. Only 27% of the time is spent doing the actual computation.

The only way I see to improve performance is to change _get_cythonized_result to a 2d operation, operating on all columns at once. In the PR that caused this regression, that's the first thing I tried and found that it got too hairy. In the process, I realized that perhaps a better solution would be to incorporate the features of _get_cythonized_result (namely, pre- and post-processing) into _cython_agg_general.

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm you are reverting.

yep. should have mentioned this in the OP.

instead I would like to see if the generic function can be patched (e.g. something is fundamentally different).

OK. will close this for now. can reopen if a better solution is not forthcoming before the release.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. will close this for now. can reopen if a better solution is not forthcoming before the release.

or maybe better would be to backport this after 1.1rc if not fixed before.

if result.ndim == 1:
result = np.sqrt(result)
else:
cols = result.columns.get_indexer_for(
result.columns.difference(self.exclusions).unique()
)
# TODO(GH-22046) - setting with iloc broken if labels are not unique
# .values to remove labels
result.iloc[:, cols] = np.sqrt(result.iloc[:, cols]).values

return result

@Substitution(name="groupby")
@Appender(_common_see_also)
Expand Down