Feature Type
-
[ ] Adding new functionality to pandas
-
[X] 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
Comment From: Pvlb1998
Take
Comment From: Pvlb1998
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.
Comment From: mroeschke
@Pvlb1998 just a heads up, please wait for confirmation from members of the core team regarding support of this functionality before moving forward.
Comment From: Pvlb1998
@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?
Comment From: rhshadrach
This looks like a duplicate of https://github.com/pandas-dev/pandas/issues/40112
Comment From: phofl
Yep agreed, closing then