Skip to content

Commit 07c627c

Browse files
API: Rename arg to func in Series.map (#61264)
1 parent 38dd653 commit 07c627c

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ Other Deprecations
421421
- Deprecated lowercase strings ``w``, ``w-mon``, ``w-tue``, etc. denoting frequencies in :class:`Week` in favour of ``W``, ``W-MON``, ``W-TUE``, etc. (:issue:`58998`)
422422
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
423423
- Deprecated strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`)
424+
- Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)
424425
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
425426

426427
.. ---------------------------------------------------------------------------

pandas/core/series.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
doc,
5353
set_module,
5454
)
55+
from pandas.util._exceptions import (
56+
find_stack_level,
57+
)
5558
from pandas.util._validators import (
5659
validate_ascending,
5760
validate_bool_kwarg,
@@ -4320,7 +4323,7 @@ def unstack(
43204323

43214324
def map(
43224325
self,
4323-
arg: Callable | Mapping | Series,
4326+
func: Callable | Mapping | Series | None = None,
43244327
na_action: Literal["ignore"] | None = None,
43254328
**kwargs,
43264329
) -> Series:
@@ -4333,8 +4336,8 @@ def map(
43334336
43344337
Parameters
43354338
----------
4336-
arg : function, collections.abc.Mapping subclass or Series
4337-
Mapping correspondence.
4339+
func : function, collections.abc.Mapping subclass or Series
4340+
Function or mapping correspondence.
43384341
na_action : {None, 'ignore'}, default None
43394342
If 'ignore', propagate NaN values, without passing them to the
43404343
mapping correspondence.
@@ -4404,9 +4407,22 @@ def map(
44044407
3 I am a rabbit
44054408
dtype: object
44064409
"""
4407-
if callable(arg):
4408-
arg = functools.partial(arg, **kwargs)
4409-
new_values = self._map_values(arg, na_action=na_action)
4410+
if func is None:
4411+
if "arg" in kwargs:
4412+
# `.map(arg=my_func)`
4413+
func = kwargs.pop("arg")
4414+
warnings.warn(
4415+
"The parameter `arg` has been renamed to `func`, and it "
4416+
"will stop being supported in a future version of pandas.",
4417+
FutureWarning,
4418+
stacklevel=find_stack_level(),
4419+
)
4420+
else:
4421+
raise ValueError("The `func` parameter is required")
4422+
4423+
if callable(func):
4424+
func = functools.partial(func, **kwargs)
4425+
new_values = self._map_values(func, na_action=na_action)
44104426
return self._constructor(new_values, index=self.index, copy=False).__finalize__(
44114427
self, method="map"
44124428
)

pandas/tests/series/methods/test_map.py

+24
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,27 @@ def test_map_kwargs():
604604
result = Series([2, 4, 5]).map(lambda x, y: x + y, y=2)
605605
expected = Series([4, 6, 7])
606606
tm.assert_series_equal(result, expected)
607+
608+
609+
def test_map_arg_as_kwarg():
610+
with tm.assert_produces_warning(
611+
FutureWarning, match="`arg` has been renamed to `func`"
612+
):
613+
Series([1, 2]).map(arg={})
614+
615+
616+
def test_map_func_and_arg():
617+
# `arg`is considered a normal kwarg that should be passed to the function
618+
result = Series([1, 2]).map(lambda _, arg: arg, arg=3)
619+
expected = Series([3, 3])
620+
tm.assert_series_equal(result, expected)
621+
622+
623+
def test_map_no_func_or_arg():
624+
with pytest.raises(ValueError, match="The `func` parameter is required"):
625+
Series([1, 2]).map()
626+
627+
628+
def test_map_func_is_none():
629+
with pytest.raises(ValueError, match="The `func` parameter is required"):
630+
Series([1, 2]).map(func=None)

0 commit comments

Comments
 (0)