Skip to content

ENH: applymap get kwargs #39987 #40562

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

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Other enhancements
- :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`)
- :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`)
- :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`)
- :meth:`DataFrame.applymap` can now accept kwargs to pass on to func (:issue:`39987`)
- Disallow :class:`DataFrame` indexer for ``iloc`` for :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__`, (:issue:`39004`)
- :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`)
- :meth:`DataFrame.plot.scatter` can now accept a categorical column as the argument to ``c`` (:issue:`12380`, :issue:`31357`)
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import collections
from collections import abc
import datetime
import functools
from io import StringIO
import itertools
import mmap
Expand Down Expand Up @@ -3332,7 +3333,6 @@ def _ixs(self, i: int, axis: int = 0):

# this is a cached value, mark it so
result._set_as_cached(label, self)

return result

def _get_column_array(self, i: int) -> ArrayLike:
Expand Down Expand Up @@ -8440,7 +8440,7 @@ def apply(
return op.apply()

def applymap(
self, func: PythonFuncType, na_action: Optional[str] = None
self, func: PythonFuncType, na_action: Optional[str] = None, **kwargs
) -> DataFrame:
"""
Apply a function to a Dataframe elementwise.
Expand All @@ -8454,6 +8454,9 @@ def applymap(
Python function, returns a single value from a single value.
na_action : {None, 'ignore'}, default None
If ‘ignore’, propagate NaN values, without passing them to func.
**kwargs
Additional keyword arguments to pass as keywords arguments to
`func`.

.. versionadded:: 1.2

Expand Down Expand Up @@ -8508,6 +8511,7 @@ def applymap(
f"na_action must be 'ignore' or None. Got {repr(na_action)}"
)
ignore_na = na_action == "ignore"
func = functools.partial(func, **kwargs)

# if we have a dtype == 'M8[ns]', provide boxed values
def infer(x):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ def test_applymap(float_frame):
tm.assert_frame_equal(result, expected)


def test_applymap_kwargs():
result = DataFrame([[1, 2], [3, 4]]).applymap(lambda x, y: x + y, y=2)
expected = DataFrame([[3, 4], [5, 6]])
tm.assert_frame_equal(result, expected)


def test_applymap_na_ignore(float_frame):
# GH 23803
strlen_frame = float_frame.applymap(lambda x: len(str(x)))
Expand Down