Skip to content

Commit 97714a7

Browse files
committed
ENH: applymap get kwargs pandas-dev#39987
1 parent 8837b36 commit 97714a7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import collections
1414
from collections import abc
1515
import datetime
16+
import functools
1617
from io import StringIO
1718
import itertools
1819
import mmap
@@ -3045,7 +3046,6 @@ def _ixs(self, i: int, axis: int = 0):
30453046

30463047
# this is a cached value, mark it so
30473048
result._set_as_cached(label, self)
3048-
30493049
return result
30503050

30513051
def _get_column_array(self, i: int) -> ArrayLike:
@@ -7934,7 +7934,7 @@ def apply(
79347934
return op.apply()
79357935

79367936
def applymap(
7937-
self, func: PythonFuncType, na_action: Optional[str] = None
7937+
self, func: PythonFuncType, na_action: Optional[str] = None, **kwargs
79387938
) -> DataFrame:
79397939
"""
79407940
Apply a function to a Dataframe elementwise.
@@ -7948,6 +7948,9 @@ def applymap(
79487948
Python function, returns a single value from a single value.
79497949
na_action : {None, 'ignore'}, default None
79507950
If ‘ignore’, propagate NaN values, without passing them to func.
7951+
**kwargs
7952+
Additional keyword arguments to pass as keywords arguments to
7953+
`func`.
79517954
79527955
.. versionadded:: 1.2
79537956
@@ -8002,6 +8005,7 @@ def applymap(
80028005
f"na_action must be 'ignore' or None. Got {repr(na_action)}"
80038006
)
80048007
ignore_na = na_action == "ignore"
8008+
func = functools.partial(func, **kwargs)
80058009

80068010
# if we have a dtype == 'M8[ns]', provide boxed values
80078011
def infer(x):

pandas/tests/apply/test_frame_apply.py

+6
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ def test_applymap(float_frame):
678678
tm.assert_frame_equal(result, frame)
679679

680680

681+
def test_applymap_kwargs():
682+
result = pd.DataFrame([[1, 2], [3, 4]]).applymap(lambda x, y: x + y, y=2)
683+
expected = pd.DataFrame([[3, 4], [5, 6]])
684+
assert tm.assert_frame_equal(result, expected)
685+
686+
681687
def test_applymap_na_ignore(float_frame):
682688
# GH 23803
683689
strlen_frame = float_frame.applymap(lambda x: len(str(x)))

0 commit comments

Comments
 (0)