Skip to content

ENH: Add support for *args in apply-like functions. #48582

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
1 of 3 tasks
amotzop opened this issue Sep 16, 2022 · 6 comments
Closed
1 of 3 tasks

ENH: Add support for *args in apply-like functions. #48582

amotzop opened this issue Sep 16, 2022 · 6 comments
Labels
Apply Apply, Aggregate, Transform, Map Closing Candidate May be closeable, needs more eyeballs Enhancement

Comments

@amotzop
Copy link
Contributor

amotzop commented Sep 16, 2022

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I think it would be nice if all the various apply-like functions had the same way to pass extra arguments for the applied function.
The state now is as follows:
Series/DataFrame.apply - can accept **kwargs, but to pass non-keyword argument there you pass a tuple args, instead of *args.
DataFrame.applymap - can accept **kwargs, but there is no way to pass non-keyword arguments.
Index/Series.map - no way to pass any additional arguments.
GroupBy.apply - can pass arguments using *args, **kwargs.
GroupBy/Series/DataFrame.transform - can pass arguments using *args, **kwargs.

I think that the *args, **kwargs way is the cleanest, so I propose to change the implementation of all other cases to conform to it.

I also think that passing extra arguments to Series/DataFrame.apply using the args tuple should be deprecated (but while you have both options they should be mutually exclusive).

Feature Description

The implementation seems easy enough/already exists in most cases.
For example in the Series.apply case:

def apply(func, convert_dtype=True, args=(), *nargs, **kwargs):
    if args != () and nargs != ():
        raise ValueError("Can only pass extra arguments using args or nargs, not both")
    args = args if args != () else nargs
    return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

Alternative Solutions

As I said, it seems that in many places this change is already implemented and just needs to be applied everywhere.

Additional Context

No response

@amotzop amotzop added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 16, 2022
@Pvlb1998
Copy link

Take

@Pvlb1998
Copy link

Pvlb1998 commented Nov 3, 2022

Are the functions mentioned on the description (like Series/DataFrame.apply, DataFrame.applymap...) the only places where we need to apply the code on the solution?

Also, is there any possibility to show some examples where I can test these functions? It's just to make sure the code is working properly after the modifications.

@mroeschke
Copy link
Member

@Pvlb1998 just a heads up, please wait for confirmation from members of the core team regarding support of this functionality before moving forward.

@Pvlb1998
Copy link

Pvlb1998 commented Nov 6, 2022

@mroeschke Oh it's alright! Even if this function doesn't properly work, I would still like to work on it purely for learning (doesn't have to be aproved). But even after making some modifications I still don't know how to test it. If it's possible, could you provide some cases of test so I can at least check if there's anything wrong with my modifications?

@rhshadrach
Copy link
Member

This looks like a duplicate of #40112

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 15, 2022
@phofl
Copy link
Member

phofl commented Dec 18, 2022

Yep agreed, closing then

@phofl phofl closed this as completed Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Closing Candidate May be closeable, needs more eyeballs Enhancement
Projects
None yet
Development

No branches or pull requests

5 participants