Skip to content
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

ENH: Groupby agg support multiple funcs numba #53486

Merged
merged 4 commits into from
Jun 2, 2023

Conversation

lithomas1
Copy link
Member

@lithomas1 lithomas1 commented Jun 1, 2023

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Only thing missing for now is Groupby.agg with multiple UDFs. This'll require changes to the shared apply code, so I've left that out of this PR.
(I've added a commented out example test case to address this).

@lithomas1 lithomas1 added Enhancement Groupby numba numba-accelerated operations labels Jun 1, 2023
if is_groupby:
engine = self.kwargs.get("engine", None)
engine_kwargs = self.kwargs.get("engine_kwargs", None)
kwargs = {"engine": engine, "engine_kwargs": engine_kwargs}
Copy link
Member

Choose a reason for hiding this comment

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

Can we update the method signatures with by adding engine and engine_kwargs params instead of mangling kwargs?

Copy link
Member Author

Choose a reason for hiding this comment

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

obj here can be a Series/DF/Resampler/ too, since the code is shared with there, so it wouldn't be possible to add engine/engine_kwargs there (unless we supported numba there too), since agg there is part of the public API.

Copy link
Member Author

Choose a reason for hiding this comment

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

obj here can be a Series/DF/Resampler/ too, since the code is shared with there, so it wouldn't be possible to add engine/engine_kwargs there (unless we supported numba there too), since agg there is part of the public API.

Copy link
Member

Choose a reason for hiding this comment

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

The more we evolve groupby, the more I realize trying to share this code between Series/DataFrame and groupby was a mistake of mine. I've been thinking we should refactor and separate off groupby entirely (but it can still live in core.apply). I'm okay with having a bit of a kludge here and will separate/cleanup after.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

The comment in the OP ("so I've left that out of this PR") is out dated now, right? I didn't see the commented out test; it's been changed to an xfail. Got it.

if is_groupby:
engine = self.kwargs.get("engine", None)
engine_kwargs = self.kwargs.get("engine_kwargs", None)
kwargs = {"engine": engine, "engine_kwargs": engine_kwargs}
Copy link
Member

Choose a reason for hiding this comment

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

The more we evolve groupby, the more I realize trying to share this code between Series/DataFrame and groupby was a mistake of mine. I've been thinking we should refactor and separate off groupby entirely (but it can still live in core.apply). I'm okay with having a bit of a kludge here and will separate/cleanup after.

pandas/tests/groupby/aggregate/test_numba.py Outdated Show resolved Hide resolved
@mroeschke mroeschke added this to the 2.1 milestone Jun 1, 2023
@@ -1416,6 +1424,17 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
result.columns = columns # type: ignore[assignment]

if result is None:
# Remove the kwargs we inserted
Copy link
Member

Choose a reason for hiding this comment

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

So we need to go through result = op.agg() first instead of just using self._aggregate_with_numba in the if if maybe_use_numba(engine) branch above?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that checks for str/list/dict aggregations, and call back into the agg function itself.

It'll return None, if a callable is passed, though (what this block handles).

@mroeschke
Copy link
Member

Looks pretty good. Just a merge conflict

@mroeschke mroeschke merged commit 76e02e4 into pandas-dev:main Jun 2, 2023
@mroeschke
Copy link
Member

Thanks @lithomas1

@lithomas1 lithomas1 deleted the numba-multiagg branch June 2, 2023 21:05
topper-123 pushed a commit to topper-123/pandas that referenced this pull request Jun 5, 2023
* ENH: Groupby agg support multiple funcs numba

* address code review

* remove old TODO
Daquisu pushed a commit to Daquisu/pandas that referenced this pull request Jul 8, 2023
* ENH: Groupby agg support multiple funcs numba

* address code review

* remove old TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Groupby numba numba-accelerated operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants