Skip to content

Commit bd31d64

Browse files
authored
BUG: Series map ignoring na_action for dict or series mapper (pandas-dev#47585)
* BUG: Series map ignoring na_action for dict or series mapper * Add tests
1 parent 0ce2921 commit bd31d64

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ Missing
914914
^^^^^^^
915915
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``downcast`` keyword not being respected in some cases where there are no NA values present (:issue:`45423`)
916916
- Bug in :meth:`Series.fillna` and :meth:`DataFrame.fillna` with :class:`IntervalDtype` and incompatible value raising instead of casting to a common (usually object) dtype (:issue:`45796`)
917+
- Bug in :meth:`Series.map` not respecting ``na_action`` argument if mapper is a ``dict`` or :class:`Series` (:issue:`47527`)
917918
- Bug in :meth:`DataFrame.interpolate` with object-dtype column not returning a copy with ``inplace=False`` (:issue:`45791`)
918919
- Bug in :meth:`DataFrame.dropna` allows to set both ``how`` and ``thresh`` incompatible arguments (:issue:`46575`)
919920
- Bug in :meth:`DataFrame.fillna` ignored ``axis`` when :class:`DataFrame` is single block (:issue:`47713`)

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ def _map_values(self, mapper, na_action=None):
843843
f"{na_action} was passed"
844844
)
845845
raise ValueError(msg)
846+
847+
if na_action == "ignore":
848+
mapper = mapper[mapper.index.notna()]
849+
846850
# Since values were input this means we came from either
847851
# a dict or a series and mapper should be an index
848852
if is_categorical_dtype(self.dtype):

pandas/tests/apply/test_series_apply.py

+28
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,34 @@ def test_map_dict_na_key():
598598
tm.assert_series_equal(result, expected)
599599

600600

601+
@pytest.mark.parametrize("arg_func", [dict, Series])
602+
def test_map_dict_ignore_na(arg_func):
603+
# GH#47527
604+
mapping = arg_func({1: 10, np.nan: 42})
605+
ser = Series([1, np.nan, 2])
606+
result = ser.map(mapping, na_action="ignore")
607+
expected = Series([10, np.nan, np.nan])
608+
tm.assert_series_equal(result, expected)
609+
610+
611+
def test_map_defaultdict_ignore_na():
612+
# GH#47527
613+
mapping = defaultdict(int, {1: 10, np.nan: 42})
614+
ser = Series([1, np.nan, 2])
615+
result = ser.map(mapping)
616+
expected = Series([10, 0, 0])
617+
tm.assert_series_equal(result, expected)
618+
619+
620+
def test_map_categorical_na_ignore():
621+
# GH#47527
622+
values = pd.Categorical([1, np.nan, 2], categories=[10, 1])
623+
ser = Series(values)
624+
result = ser.map({1: 10, np.nan: 42})
625+
expected = Series([10, np.nan, np.nan])
626+
tm.assert_series_equal(result, expected)
627+
628+
601629
def test_map_dict_subclass_with_missing():
602630
"""
603631
Test Series.map with a dictionary subclass that defines __missing__,

0 commit comments

Comments
 (0)