Skip to content

Commit 4e0d0b4

Browse files
authored
ENH: applymap get kwargs #39987 (#40562)
1 parent 86d5980 commit 4e0d0b4

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Diff for: doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Other enhancements
128128
- :meth:`.Rolling.sum`, :meth:`.Expanding.sum`, :meth:`.Rolling.mean`, :meth:`.Expanding.mean`, :meth:`.Rolling.median`, :meth:`.Expanding.median`, :meth:`.Rolling.max`, :meth:`.Expanding.max`, :meth:`.Rolling.min`, and :meth:`.Expanding.min` now support ``Numba`` execution with the ``engine`` keyword (:issue:`38895`)
129129
- :meth:`DataFrame.apply` can now accept NumPy unary operators as strings, e.g. ``df.apply("sqrt")``, which was already the case for :meth:`Series.apply` (:issue:`39116`)
130130
- :meth:`DataFrame.apply` can now accept non-callable DataFrame properties as strings, e.g. ``df.apply("size")``, which was already the case for :meth:`Series.apply` (:issue:`39116`)
131+
- :meth:`DataFrame.applymap` can now accept kwargs to pass on to func (:issue:`39987`)
131132
- Disallow :class:`DataFrame` indexer for ``iloc`` for :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__`, (:issue:`39004`)
132133
- :meth:`Series.apply` can now accept list-like or dictionary-like arguments that aren't lists or dictionaries, e.g. ``ser.apply(np.array(["sum", "mean"]))``, which was already the case for :meth:`DataFrame.apply` (:issue:`39140`)
133134
- :meth:`DataFrame.plot.scatter` can now accept a categorical column as the argument to ``c`` (:issue:`12380`, :issue:`31357`)

Diff for: pandas/core/frame.py

+9-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
@@ -3332,7 +3333,6 @@ def _ixs(self, i: int, axis: int = 0):
33323333

33333334
# this is a cached value, mark it so
33343335
result._set_as_cached(label, self)
3335-
33363336
return result
33373337

33383338
def _get_column_array(self, i: int) -> ArrayLike:
@@ -8440,7 +8440,7 @@ def apply(
84408440
return op.apply()
84418441

84428442
def applymap(
8443-
self, func: PythonFuncType, na_action: Optional[str] = None
8443+
self, func: PythonFuncType, na_action: Optional[str] = None, **kwargs
84448444
) -> DataFrame:
84458445
"""
84468446
Apply a function to a Dataframe elementwise.
@@ -8457,6 +8457,12 @@ def applymap(
84578457
84588458
.. versionadded:: 1.2
84598459
8460+
**kwargs
8461+
Additional keyword arguments to pass as keywords arguments to
8462+
`func`.
8463+
8464+
.. versionadded:: 1.3
8465+
84608466
Returns
84618467
-------
84628468
DataFrame
@@ -8508,6 +8514,7 @@ def applymap(
85088514
f"na_action must be 'ignore' or None. Got {repr(na_action)}"
85098515
)
85108516
ignore_na = na_action == "ignore"
8517+
func = functools.partial(func, **kwargs)
85118518

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

Diff for: pandas/tests/apply/test_frame_apply.py

+7
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ def test_applymap(float_frame):
560560
tm.assert_frame_equal(result, expected)
561561

562562

563+
def test_applymap_kwargs():
564+
# GH 40652
565+
result = DataFrame([[1, 2], [3, 4]]).applymap(lambda x, y: x + y, y=2)
566+
expected = DataFrame([[3, 4], [5, 6]])
567+
tm.assert_frame_equal(result, expected)
568+
569+
563570
def test_applymap_na_ignore(float_frame):
564571
# GH 23803
565572
strlen_frame = float_frame.applymap(lambda x: len(str(x)))

0 commit comments

Comments
 (0)