Skip to content

ENH: pd.NamedAgg forwards *args and **kwargs to aggfunc #58852

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

Closed
wants to merge 13 commits into from

Conversation

tomytp
Copy link
Contributor

@tomytp tomytp commented May 28, 2024

@tomytp tomytp requested a review from rhshadrach as a code owner May 28, 2024 22:41
@tomytp tomytp marked this pull request as draft May 29, 2024 00:13
@tomytp tomytp marked this pull request as ready for review May 29, 2024 01:33
@tomytp tomytp changed the title pd.NamedAgg forwards *args and **kwargs to aggfunc ENH: pd.NamedAgg forwards *args and **kwargs to aggfunc May 29, 2024
@@ -108,7 +108,12 @@
ScalarResult = TypeVar("ScalarResult")


class NamedAgg(NamedTuple):
class _BaseNamedAgg(NamedTuple):
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for introducing a new class here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's because you cannot override new when inheriting from NamedTuple directly.

Copy link
Contributor Author

@tomytp tomytp May 31, 2024

Choose a reason for hiding this comment

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

So the most elegant way I found to override it while maintaining the NamedTuple properties was to Introduce a new class. Do you have a suggestion for another method to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the reference error for when I tried to inherit directly from NamedTuple is:
AttributeError: Cannot overwrite NamedTuple attribute __new__

Copy link
Member

@WillAyd WillAyd May 31, 2024

Choose a reason for hiding this comment

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

Ah that's unfortunate. This seems kind of hacky as a result though - maybe we can just drop the NamedTuple subclass? I think that was mostly developed that way as a convenience rather than an intentional design decision - I am not sure why NamedAgg would need to be "tuple-ish"

@rhshadrach might have more thoughts

Copy link
Member

@rhshadrach rhshadrach Jun 1, 2024

Choose a reason for hiding this comment

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

python/cpython#77258 has some relevant information, especially the linked discussion there to the issue in python/typing.

In any case, it looks to me like this implementation just ignores args / kwargs when the provided aggfunc is not a callable? If that's the case, then I'm -1 on this approach anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rhshadrach I'll rethink the approach to include the function names passed as string.

I'm not sure I understand your thoughts on the necessity of keeping NamedArgs with tuple behavior. Could you clarify that?

TY

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand your thoughts on the necessity of keeping NamedArgs with tuple behavior. Could you clarify that?

I don't believe I shared any thoughts in this regard, just a link to some background information on why Python raises when you try to overwrite __new__.

@mroeschke mroeschke added the Apply Apply, Aggregate, Transform, Map label May 31, 2024
@tomytp tomytp requested a review from WillAyd May 31, 2024 18:59
Copy link
Contributor

github-actions bot commented Jul 2, 2024

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Jul 2, 2024
@rhshadrach
Copy link
Member

Closing as stale. @tomytp - if you'd like to continue, comment here and we can reopen.

@rhshadrach rhshadrach closed this Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: pd.NamedAgg should accept args and kwargs
4 participants