@@ -322,6 +322,19 @@ def test_map_dict_na_key():
322322 tm .assert_series_equal (result , expected )
323323
324324
325+ def test_map_missing_key (na_action ):
326+ s = Series ([1 , 2 , 42 ])
327+ mapping = {1 : "a" , 2 : "b" , 3 : "c" }
328+
329+ if na_action == "raise" :
330+ with pytest .raises (ValueError ):
331+ s .map (mapping , na_action = na_action )
332+ else :
333+ expected = Series (["a" , "b" , np .nan ])
334+ result = s .map (mapping , na_action = na_action )
335+ tm .assert_series_equal (result , expected )
336+
337+
325338def test_map_defaultdict_na_key (na_action ):
326339 # GH 48813
327340 s = Series ([1 , 2 , np .nan ])
@@ -380,7 +393,7 @@ def test_map_categorical_na_ignore(na_action, expected):
380393 tm .assert_series_equal (result , expected )
381394
382395
383- def test_map_dict_subclass_with_missing ():
396+ def test_map_dict_subclass_with_missing (na_action ):
384397 """
385398 Test Series.map with a dictionary subclass that defines __missing__,
386399 i.e. sets a default value (GH #15999).
@@ -392,30 +405,40 @@ def __missing__(self, key):
392405
393406 s = Series ([1 , 2 , 3 ])
394407 dictionary = DictWithMissing ({3 : "three" })
395- result = s .map (dictionary )
408+ result = s .map (dictionary , na_action = na_action ) # also works with 'raise'
396409 expected = Series (["missing" , "missing" , "three" ])
397410 tm .assert_series_equal (result , expected )
398411
399412
400- def test_map_dict_subclass_without_missing ():
413+ def test_map_dict_subclass_without_missing (na_action ):
401414 class DictWithoutMissing (dict ):
402415 pass
403416
404417 s = Series ([1 , 2 , 3 ])
405418 dictionary = DictWithoutMissing ({3 : "three" })
406- result = s .map (dictionary )
407- expected = Series ([np .nan , np .nan , "three" ])
408- tm .assert_series_equal (result , expected )
409419
420+ if na_action == "raise" :
421+ with pytest .raises (ValueError ):
422+ _ = s .map (dictionary , na_action = na_action )
423+ else :
424+ result = s .map (dictionary , na_action = na_action )
425+ expected = Series ([np .nan , np .nan , "three" ])
426+ tm .assert_series_equal (result , expected )
410427
411- def test_map_abc_mapping (non_dict_mapping_subclass ):
428+
429+ def test_map_abc_mapping (non_dict_mapping_subclass , na_action ):
412430 # https://github.com/pandas-dev/pandas/issues/29733
413431 # Check collections.abc.Mapping support as mapper for Series.map
414432 s = Series ([1 , 2 , 3 ])
415433 not_a_dictionary = non_dict_mapping_subclass ({3 : "three" })
416- result = s .map (not_a_dictionary )
417- expected = Series ([np .nan , np .nan , "three" ])
418- tm .assert_series_equal (result , expected )
434+
435+ if na_action == "raise" :
436+ with pytest .raises (ValueError ):
437+ _ = s .map (not_a_dictionary , na_action = na_action )
438+ else :
439+ result = s .map (not_a_dictionary , na_action = na_action )
440+ expected = Series ([np .nan , np .nan , "three" ])
441+ tm .assert_series_equal (result , expected )
419442
420443
421444def test_map_abc_mapping_with_missing (non_dict_mapping_subclass ):
0 commit comments