Skip to content

Commit 8b4491f

Browse files
authored
ERR: Raise on invalid na_action in Series.map (#32790)
1 parent d5a07a1 commit 8b4491f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ Other
405405
- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`).
406406
- :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`)
407407
- 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`)
408+
- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`)
408409
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
409410

410411
.. ---------------------------------------------------------------------------

pandas/core/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,14 @@ def _map_values(self, mapper, na_action=None):
11561156
def map_f(values, f):
11571157
return lib.map_infer_mask(values, f, isna(values).view(np.uint8))
11581158

1159-
else:
1159+
elif na_action is None:
11601160
map_f = lib.map_infer
1161+
else:
1162+
msg = (
1163+
"na_action must either be 'ignore' or None, "
1164+
f"{na_action} was passed"
1165+
)
1166+
raise ValueError(msg)
11611167

11621168
# mapper is a function
11631169
new_values = map_f(values, mapper)

pandas/tests/series/test_apply.py

+7
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,13 @@ def test_map_float_to_string_precision(self):
792792
expected = {0: "0.3333333333333333"}
793793
assert result == expected
794794

795+
def test_map_with_invalid_na_action_raises(self):
796+
# https://github.com/pandas-dev/pandas/issues/32815
797+
s = pd.Series([1, 2, 3])
798+
msg = "na_action must either be 'ignore' or None"
799+
with pytest.raises(ValueError, match=msg):
800+
s.map(lambda x: x, na_action="____")
801+
795802
def test_apply_to_timedelta(self):
796803
list_of_valid_strings = ["00:00:01", "00:00:02"]
797804
a = pd.to_timedelta(list_of_valid_strings)

0 commit comments

Comments
 (0)