Skip to content

Commit 4eb4be2

Browse files
authored
BUG: Series.map is ignoring the na_action keyword (#46860)
1 parent 4fcc8d5 commit 4eb4be2

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ as seen in the following example.
7979

8080
Other enhancements
8181
^^^^^^^^^^^^^^^^^^
82+
- :meth:`Series.map` now raises when ``arg`` is dict but ``na_action`` is not either ``None`` or ``'ignore'`` (:issue:`46588`)
8283
- :meth:`MultiIndex.to_frame` now supports the argument ``allow_duplicates`` and raises on duplicate labels if it is missing or False (:issue:`45245`)
8384
- :class:`StringArray` now accepts array-likes containing nan-likes (``None``, ``np.nan``) for the ``values`` parameter in its constructor in addition to strings and :attr:`pandas.NA`. (:issue:`40839`)
8485
- Improved the rendering of ``categories`` in :class:`CategoricalIndex` (:issue:`45218`)

pandas/core/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,12 @@ def _map_values(self, mapper, na_action=None):
842842
)
843843

844844
if isinstance(mapper, ABCSeries):
845+
if na_action not in (None, "ignore"):
846+
msg = (
847+
"na_action must either be 'ignore' or None, "
848+
f"{na_action} was passed"
849+
)
850+
raise ValueError(msg)
845851
# Since values were input this means we came from either
846852
# a dict or a series and mapper should be an index
847853
if is_categorical_dtype(self.dtype):

pandas/tests/apply/test_invalid_arg.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ def test_map_with_invalid_na_action_raises():
6666
s.map(lambda x: x, na_action="____")
6767

6868

69+
@pytest.mark.parametrize("input_na_action", ["____", True])
70+
def test_map_arg_is_dict_with_invalid_na_action_raises(input_na_action):
71+
# https://github.com/pandas-dev/pandas/issues/46588
72+
s = Series([1, 2, 3])
73+
msg = f"na_action must either be 'ignore' or None, {input_na_action} was passed"
74+
with pytest.raises(ValueError, match=msg):
75+
s.map({1: 2}, na_action=input_na_action)
76+
77+
6978
def test_map_categorical_na_action():
7079
values = Categorical(list("ABBABCD"), categories=list("DCBA"), ordered=True)
7180
s = Series(values, name="XX", index=list("abcdefg"))

0 commit comments

Comments
 (0)