Skip to content

Commit b87bf85

Browse files
ENH: Add kwargs to Series.map (pandas-dev#59843)
Co-authored-by: Naresh Kumar <[email protected]>
1 parent c8a6740 commit b87bf85

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Other enhancements
5454
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
5555
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
5656
- :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`)
57+
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
5758
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
5859
- :meth:`str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
5960
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)

pandas/core/series.py

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Mapping,
1212
Sequence,
1313
)
14+
import functools
1415
import operator
1516
import sys
1617
from textwrap import dedent
@@ -4312,6 +4313,7 @@ def map(
43124313
self,
43134314
arg: Callable | Mapping | Series,
43144315
na_action: Literal["ignore"] | None = None,
4316+
**kwargs,
43154317
) -> Series:
43164318
"""
43174319
Map values of Series according to an input mapping or function.
@@ -4327,6 +4329,11 @@ def map(
43274329
na_action : {None, 'ignore'}, default None
43284330
If 'ignore', propagate NaN values, without passing them to the
43294331
mapping correspondence.
4332+
**kwargs
4333+
Additional keyword arguments to pass as keywords arguments to
4334+
`arg`.
4335+
4336+
.. versionadded:: 3.0.0
43304337
43314338
Returns
43324339
-------
@@ -4388,6 +4395,8 @@ def map(
43884395
3 I am a rabbit
43894396
dtype: object
43904397
"""
4398+
if callable(arg):
4399+
arg = functools.partial(arg, **kwargs)
43914400
new_values = self._map_values(arg, na_action=na_action)
43924401
return self._constructor(new_values, index=self.index, copy=False).__finalize__(
43934402
self, method="map"

pandas/tests/series/methods/test_map.py

+7
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,10 @@ def test_map_type():
597597
result = s.map(type)
598598
expected = Series([int, str, type], index=["a", "b", "c"])
599599
tm.assert_series_equal(result, expected)
600+
601+
602+
def test_map_kwargs():
603+
# GH 59814
604+
result = Series([2, 4, 5]).map(lambda x, y: x + y, y=2)
605+
expected = Series([4, 6, 7])
606+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)