-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: pd.NamedAgg forwards *args and **kwargs to aggfunc #62729
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
Conversation
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
pandas/core/groupby/generic.py
Outdated
| >>> agg_1 = pd.NamedAgg(column=1, aggfunc=lambda x: np.mean(x)) | ||
| >>> df.groupby("key").agg(result_a=agg_a, result_1=agg_1) | ||
| result_a result_1 | ||
| >>> agg_b = pd.NamedAgg(column="b", aggfunc=lambda x: x.mean()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the point here is to demonstrate that you can used a named tuple on columns that are not strings.
pandas/core/groupby/generic.py
Outdated
| return original_func(series, *final_args, **final_kwargs) | ||
|
|
||
| wrapped._is_wrapped = True # type: ignore[attr-defined] | ||
| aggfunc = wrapped |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line with the above, this changes the aggfunc which is a public attribute. Instead, I think we should utilize args/kwargs in places within pandas that accept a NamedAgg
|
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
pandas/core/groupby/generic.py
Outdated
| aggfunc : function or str | ||
| Function to apply to the provided column. If string, the name of a built-in | ||
| pandas function. | ||
| *args, **kwargs : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| *args, **kwargs : | |
| *args, **kwargs : Any |
pandas/core/groupby/generic.py
Outdated
|
|
||
| column: Hashable | ||
| aggfunc: AggScalar | ||
| args: tuple[Any, ...] = dataclasses.field(default_factory=tuple) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either way is fine, just wanted to mention this could be args: tuple[Any, ...] = () since tuples are immutable.
pandas/core/apply.py
Outdated
| for key, val in kwargs.items(): | ||
| if isinstance(val, NamedAgg): | ||
| aggfunc = val.aggfunc | ||
| if getattr(val, "args", ()) or getattr(val, "kwargs", {}): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what case does val not have an args or kwargs attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the redundant checks
| expected = df.groupby("A").agg( | ||
| n_between01=("B", lambda x: x.between(0, 1).sum()), | ||
| n_between13=("B", lambda x: x.between(0, 3).sum()), | ||
| n_between02=("B", lambda x: x.between(0, 2).sum()), | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you specify expected here explicitly.
doc/source/whatsnew/v3.0.0.rst
Outdated
| - :class:`pandas.NamedAgg` now forwards any ``*args`` and ``**kwargs`` | ||
| to calls of ``aggfunc`` (:issue:`58283`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads to me like a bugfix (as if it already had *args and **kwargs, but just wasn't forwarding).
| - :class:`pandas.NamedAgg` now forwards any ``*args`` and ``**kwargs`` | |
| to calls of ``aggfunc`` (:issue:`58283`) | |
| - :class:`pandas.NamedAgg` now supports passing ``*args`` and ``**kwargs`` | |
| to calls of ``aggfunc`` (:issue:`58283`) |
rhshadrach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
|
Thanks @sreeja97! |
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.