Skip to content

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

Open
@WillAyd

Description

@WillAyd

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions