Skip to content

ENH: Add kwargs to Series.map #59843

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 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Other enhancements
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
- :meth:`DataFrame.plot.scatter` argument ``c`` now accepts a column of strings, where rows with the same string are colored identically (:issue:`16827` and :issue:`16485`)
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
- :meth:`str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Mapping,
Sequence,
)
import functools
import operator
import sys
from textwrap import dedent
Expand Down Expand Up @@ -4312,6 +4313,7 @@ def map(
self,
arg: Callable | Mapping | Series,
na_action: Literal["ignore"] | None = None,
**kwargs,
) -> Series:
"""
Map values of Series according to an input mapping or function.
Expand All @@ -4327,6 +4329,11 @@ def map(
na_action : {None, 'ignore'}, default None
If 'ignore', propagate NaN values, without passing them to the
mapping correspondence.
**kwargs
Additional keyword arguments to pass as keywords arguments to
`arg`.

.. versionadded:: 3.0.0

Returns
-------
Expand Down Expand Up @@ -4388,6 +4395,8 @@ def map(
3 I am a rabbit
dtype: object
"""
if callable(arg):
arg = functools.partial(arg, **kwargs)
new_values = self._map_values(arg, na_action=na_action)
return self._constructor(new_values, index=self.index, copy=False).__finalize__(
self, method="map"
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,10 @@ def test_map_type():
result = s.map(type)
expected = Series([int, str, type], index=["a", "b", "c"])
tm.assert_series_equal(result, expected)


def test_map_kwargs():
# GH 59814
result = Series([2, 4, 5]).map(lambda x, y: x + y, y=2)
expected = Series([4, 6, 7])
tm.assert_series_equal(result, expected)