Skip to content

ENH: applymap get kwargs? #39987

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
attack68 opened this issue Feb 23, 2021 · 1 comment · Fixed by #40562
Closed

ENH: applymap get kwargs? #39987

attack68 opened this issue Feb 23, 2021 · 1 comment · Fixed by #40562
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Milestone

Comments

@attack68
Copy link
Contributor

apply has kwargs whilst applymap does not. Is there a specific reason for this?

it's straight forward to do

apply(f, **kwarrgs)

or

g = partial(f **kwargs)
applymap(g)

its not documented how this can be achieved with partial which the more casual user might miss?

@attack68 attack68 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 23, 2021
@alexprincel
Copy link
Contributor

alexprincel commented Mar 22, 2021

I may attempt a PR on this unless someone disagrees with the approach but it seems like the simplest solution would be to modify the func variable in pandas.frame.applymap to be the result of functools.partial(func, **kwargs). Obviously, this requires adding **kwargs as an argument, modyfing the docs and adding tests.

I tested locally the aforementioned change and obtained this output from the following interactive session:

>>> def test(x, b=2):
...    return x + b
... 
>>> df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>>> df.applymap(test, b=5)
   a  b
0  6  8
1  7  9
>>> df.applymap(test)
   a  b
0  3  5
1  4  6
>>> df.applymap(lambda x, y: x + y, y = 2)
   a  b
0  3  5
1  4  6

alexprincel added a commit to alexprincel/pandas that referenced this issue Mar 22, 2021
alexprincel added a commit to alexprincel/pandas that referenced this issue Mar 22, 2021
alexprincel added a commit to alexprincel/pandas that referenced this issue Mar 22, 2021
@jreback jreback added this to the 1.3 milestone Mar 22, 2021
jreback pushed a commit that referenced this issue Mar 23, 2021
vladu pushed a commit to vladu/pandas that referenced this issue Apr 5, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this issue Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants