Skip to content

Conversation

@sreeja97
Copy link
Contributor

@sreeja97 sreeja97 commented Oct 18, 2025

@sreeja97 sreeja97 requested a review from rhshadrach as a code owner October 18, 2025 02:17
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.

Thanks for the PR!

>>> 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())
Copy link
Member

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.

return original_func(series, *final_args, **final_kwargs)

wrapped._is_wrapped = True # type: ignore[attr-defined]
aggfunc = wrapped
Copy link
Member

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

@sreeja97
Copy link
Contributor Author

pre-commit.ci autofix

@sreeja97 sreeja97 requested a review from rhshadrach October 29, 2025 01:53
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.

Looking good!

aggfunc : function or str
Function to apply to the provided column. If string, the name of a built-in
pandas function.
*args, **kwargs :
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
*args, **kwargs :
*args, **kwargs : Any


column: Hashable
aggfunc: AggScalar
args: tuple[Any, ...] = dataclasses.field(default_factory=tuple)
Copy link
Member

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.

for key, val in kwargs.items():
if isinstance(val, NamedAgg):
aggfunc = val.aggfunc
if getattr(val, "args", ()) or getattr(val, "kwargs", {}):
Copy link
Member

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?

Copy link
Contributor Author

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

Comment on lines 913 to 917
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()),
)
Copy link
Member

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.

Comment on lines 159 to 160
- :class:`pandas.NamedAgg` now forwards any ``*args`` and ``**kwargs``
to calls of ``aggfunc`` (:issue:`58283`)
Copy link
Member

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).

Suggested change
- :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`)

@sreeja97 sreeja97 requested a review from rhshadrach November 4, 2025 00:56
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.

lgtm

@rhshadrach rhshadrach added Reduction Operations sum, mean, min, max, etc. Enhancement labels Nov 4, 2025
@rhshadrach rhshadrach added this to the 3.0 milestone Nov 4, 2025
@rhshadrach rhshadrach added Groupby Apply Apply, Aggregate, Transform, Map and removed Reduction Operations sum, mean, min, max, etc. labels Nov 4, 2025
@rhshadrach rhshadrach merged commit af63a1c into pandas-dev:main Nov 4, 2025
62 checks passed
@rhshadrach
Copy link
Member

Thanks @sreeja97!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apply Apply, Aggregate, Transform, Map Enhancement Groupby

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: pd.NamedAgg should accept args and kwargs

2 participants