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: pd.NamedAgg should accept args and kwargs #58283

Open
1 of 3 tasks
WillAyd opened this issue Apr 16, 2024 · 4 comments
Open
1 of 3 tasks

ENH: pd.NamedAgg should accept args and kwargs #58283

WillAyd opened this issue Apr 16, 2024 · 4 comments
Assignees
Labels
Apply Apply, Aggregate, Transform Enhancement Groupby

Comments

@WillAyd
Copy link
Member

WillAyd commented Apr 16, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

This part of the API seems inconsistent:

In [24]: df = pd.DataFrame({"group": list("aab"), "val1": range(3)})
In [28]: def n_between(ser, low, high):
    ...:     return ser.between(low, high).sum()
    ...: 

In [29]: df.groupby("group")["val1"].agg(n_between, 0, 1)  # works
Out[29]: 
group
a    2
b    0
Name: val1, dtype: int64

In [30]: df.groupby("group").agg(n_between=pd.NamedAgg("val1", n_between, 0, 1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[30], line 1
----> 1 df.groupby("group").agg(n_between=pd.NamedAgg("val1", n_between, 0, 1))

TypeError: NamedAgg.__new__() takes 3 positional arguments but 5 were given

Feature Description

NamedAgg should forward along *args and **kwargs just like the normal callable function application can

Alternative Solutions

status quo

Additional Context

No response

@rhshadrach
Copy link
Member

Perhaps it's obvious, but feels worth stating: this would also enable the use of a list with different args/kwargs for each element.

+1

@rhshadrach rhshadrach added the Apply Apply, Aggregate, Transform label Apr 17, 2024
@tomhoq
Copy link
Contributor

tomhoq commented Apr 18, 2024

take

@haiyashah
Copy link

take

@tomhoq
Copy link
Contributor

tomhoq commented Apr 19, 2024

Hi @haiyashah ! I already had this issue assigned and in the meanwhile have put some time into it. Would you mind taking another issue instead to avoid one of us wasting their time?
Thank you for your understanding,
Tomaz

@tomhoq tomhoq removed their assignment Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Enhancement Groupby
Projects
None yet
Development

No branches or pull requests

4 participants