@@ -598,6 +598,36 @@ def test_map_dict_na_key():
598
598
tm .assert_series_equal (result , expected )
599
599
600
600
601
+ @pytest .mark .parametrize ("na_action" , [None , "ignore" ])
602
+ def test_map_defaultdict_na_key (na_action ):
603
+ # GH 48813
604
+ s = Series ([1 , 2 , np .nan ])
605
+ default_map = defaultdict (lambda : "missing" , {1 : "a" , 2 : "b" , np .nan : "c" })
606
+ result = s .map (default_map , na_action = na_action )
607
+ expected = Series ({0 : "a" , 1 : "b" , 2 : "c" if na_action is None else np .nan })
608
+ tm .assert_series_equal (result , expected )
609
+
610
+
611
+ @pytest .mark .parametrize ("na_action" , [None , "ignore" ])
612
+ def test_map_defaultdict_missing_key (na_action ):
613
+ # GH 48813
614
+ s = Series ([1 , 2 , np .nan ])
615
+ default_map = defaultdict (lambda : "missing" , {1 : "a" , 2 : "b" , 3 : "c" })
616
+ result = s .map (default_map , na_action = na_action )
617
+ expected = Series ({0 : "a" , 1 : "b" , 2 : "missing" if na_action is None else np .nan })
618
+ tm .assert_series_equal (result , expected )
619
+
620
+
621
+ @pytest .mark .parametrize ("na_action" , [None , "ignore" ])
622
+ def test_map_defaultdict_unmutated (na_action ):
623
+ # GH 48813
624
+ s = Series ([1 , 2 , np .nan ])
625
+ default_map = defaultdict (lambda : "missing" , {1 : "a" , 2 : "b" , np .nan : "c" })
626
+ expected_default_map = default_map .copy ()
627
+ s .map (default_map , na_action = na_action )
628
+ assert default_map == expected_default_map
629
+
630
+
601
631
@pytest .mark .parametrize ("arg_func" , [dict , Series ])
602
632
def test_map_dict_ignore_na (arg_func ):
603
633
# GH#47527
@@ -613,7 +643,7 @@ def test_map_defaultdict_ignore_na():
613
643
mapping = defaultdict (int , {1 : 10 , np .nan : 42 })
614
644
ser = Series ([1 , np .nan , 2 ])
615
645
result = ser .map (mapping )
616
- expected = Series ([10 , 0 , 0 ])
646
+ expected = Series ([10 , 42 , 0 ])
617
647
tm .assert_series_equal (result , expected )
618
648
619
649
0 commit comments