@@ -322,6 +322,19 @@ def test_map_dict_na_key():
322
322
tm .assert_series_equal (result , expected )
323
323
324
324
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
+
325
338
def test_map_defaultdict_na_key (na_action ):
326
339
# GH 48813
327
340
s = Series ([1 , 2 , np .nan ])
@@ -380,7 +393,7 @@ def test_map_categorical_na_ignore(na_action, expected):
380
393
tm .assert_series_equal (result , expected )
381
394
382
395
383
- def test_map_dict_subclass_with_missing ():
396
+ def test_map_dict_subclass_with_missing (na_action ):
384
397
"""
385
398
Test Series.map with a dictionary subclass that defines __missing__,
386
399
i.e. sets a default value (GH #15999).
@@ -392,30 +405,40 @@ def __missing__(self, key):
392
405
393
406
s = Series ([1 , 2 , 3 ])
394
407
dictionary = DictWithMissing ({3 : "three" })
395
- result = s .map (dictionary )
408
+ result = s .map (dictionary , na_action = na_action ) # also works with 'raise'
396
409
expected = Series (["missing" , "missing" , "three" ])
397
410
tm .assert_series_equal (result , expected )
398
411
399
412
400
- def test_map_dict_subclass_without_missing ():
413
+ def test_map_dict_subclass_without_missing (na_action ):
401
414
class DictWithoutMissing (dict ):
402
415
pass
403
416
404
417
s = Series ([1 , 2 , 3 ])
405
418
dictionary = DictWithoutMissing ({3 : "three" })
406
- result = s .map (dictionary )
407
- expected = Series ([np .nan , np .nan , "three" ])
408
- tm .assert_series_equal (result , expected )
409
419
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 )
410
427
411
- def test_map_abc_mapping (non_dict_mapping_subclass ):
428
+
429
+ def test_map_abc_mapping (non_dict_mapping_subclass , na_action ):
412
430
# https://github.com/pandas-dev/pandas/issues/29733
413
431
# Check collections.abc.Mapping support as mapper for Series.map
414
432
s = Series ([1 , 2 , 3 ])
415
433
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 )
419
442
420
443
421
444
def test_map_abc_mapping_with_missing (non_dict_mapping_subclass ):
0 commit comments