diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 720ce7af47a18..346b603344e9a 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -405,6 +405,7 @@ Other - Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`). - :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`) - Fixed bug in :func:`pandas.testing.assert_series_equal` where dtypes were checked for ``Interval`` and ``ExtensionArray`` operands when ``check_dtype`` was ``False`` (:issue:`32747`) +- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`) - Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/base.py b/pandas/core/base.py index e1c6bef66239d..de9c0ca1ccd78 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1156,8 +1156,14 @@ def _map_values(self, mapper, na_action=None): def map_f(values, f): return lib.map_infer_mask(values, f, isna(values).view(np.uint8)) - else: + elif na_action is None: map_f = lib.map_infer + else: + msg = ( + "na_action must either be 'ignore' or None, " + f"{na_action} was passed" + ) + raise ValueError(msg) # mapper is a function new_values = map_f(values, mapper) diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index dbe3ca27fa06d..f3717ff895a59 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -788,6 +788,13 @@ def test_map_float_to_string_precision(self): expected = {0: "0.3333333333333333"} assert result == expected + def test_map_with_invalid_na_action_raises(self): + # https://github.com/pandas-dev/pandas/issues/32815 + s = pd.Series([1, 2, 3]) + msg = "na_action must either be 'ignore' or None" + with pytest.raises(ValueError, match=msg): + s.map(lambda x: x, na_action="____") + def test_apply_to_timedelta(self): list_of_valid_strings = ["00:00:01", "00:00:02"] a = pd.to_timedelta(list_of_valid_strings)