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.transform should accept similar arguments to GroupBy.agg #58318

Open
1 of 3 tasks
WillAyd opened this issue Apr 18, 2024 · 5 comments
Open
1 of 3 tasks

ENH: GroupBy.transform should accept similar arguments to GroupBy.agg #58318

WillAyd opened this issue Apr 18, 2024 · 5 comments
Assignees
Labels
Apply Apply, Aggregate, Transform, Map Enhancement Groupby

Comments

@WillAyd
Copy link
Member

WillAyd commented Apr 18, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

In [8]: df = pd.DataFrame({"col": list("aab"), "val": range(3)})
In [8]: df.groupby("col").agg(["sum", "min"])
Out[8]: 
    val    
    sum min
col        
a     1   0
b     2   2
In [9]: df.groupby("col").transform(["sum", "min"])
TypeError: 'list' object is not callable

Feature Description

To get people out of the mindset of always using apply, it would be nice if .transform supported the same call signature as .agg

Additionally it would be great to support NamedAgg somehow in transform. The naming is a bit unfortunate...we could create a NamedTransform alias or maybe just live with the API inconsistency of:

.agg(col_a=NamedAgg("col", "sum"), ...)
.transform(col_a=NamedAgg("col", "cumsum"), ...)

I think either is still a net improvement over today

Alternative Solutions

n/a

Additional Context

No response

@WillAyd WillAyd added Enhancement Groupby Apply Apply, Aggregate, Transform, Map labels Apr 18, 2024
@abeltavares
Copy link
Contributor

take

@rhshadrach
Copy link
Member

rhshadrach commented Apr 18, 2024

I'm positive here, but there are potentially issues with agg and list-likes and we need to decide if we want to align to agg's behavior, or diverge from it. Namely, agg with a list-like will break up a DataFrame into Series first (even with a string like "sum"), and then operate on the Series.

Ref:

@abeltavares
Copy link
Contributor

abeltavares commented Apr 20, 2024

@WillAyd @rhshadrach Hi guys, the GroupBy.transform repeat aggregate values for same number of rows like original DataFrame, as opposite to GroupBy.agg, that behaviour is to be kept right?

@abeltavares
Copy link
Contributor

@WillAyd @rhshadrach Hi guys, the GroupBy.transform repeat aggregate values for same number of rows like original DataFrame, as opposite to GroupBy.agg, that behaviour is to be kept right?

@WillAyd @rhshadrach can you confirm that?

@rhshadrach
Copy link
Member

@abeltavares - that is correct.

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 a pull request may close this issue.

3 participants