@@ -1627,7 +1627,7 @@ def union_with_duplicates(
1627
1627
def map_array (
1628
1628
arr : ArrayLike ,
1629
1629
mapper ,
1630
- na_action : Literal ["ignore" ] | None = None ,
1630
+ na_action : Literal ["ignore" , "raise" ] | None = None ,
1631
1631
) -> np .ndarray | ExtensionArray | Index :
1632
1632
"""
1633
1633
Map values using an input mapping or function.
@@ -1636,9 +1636,10 @@ def map_array(
1636
1636
----------
1637
1637
mapper : function, dict, or Series
1638
1638
Mapping correspondence.
1639
- na_action : {None, 'ignore'}, default None
1639
+ na_action : {None, 'ignore', 'raise' }, default None
1640
1640
If 'ignore', propagate NA values, without passing them to the
1641
- mapping correspondence.
1641
+ mapping correspondence. If 'raise', an error is raised when the
1642
+ mapping correspondence does not cover all elements in the array.
1642
1643
1643
1644
Returns
1644
1645
-------
@@ -1647,7 +1648,7 @@ def map_array(
1647
1648
If the function returns a tuple with more than one element
1648
1649
a MultiIndex will be returned.
1649
1650
"""
1650
- if na_action not in (None , "ignore" ):
1651
+ if na_action not in (None , "ignore" , "raise" ):
1651
1652
msg = f"na_action must either be 'ignore' or None, { na_action } was passed"
1652
1653
raise ValueError (msg )
1653
1654
@@ -1686,6 +1687,11 @@ def map_array(
1686
1687
# Since values were input this means we came from either
1687
1688
# a dict or a series and mapper should be an index
1688
1689
indexer = mapper .index .get_indexer (arr )
1690
+
1691
+ if na_action == "raise" and (indexer == - 1 ).any ():
1692
+ raise ValueError ("Provided mapping is not sufficient to cover"
1693
+ "all values in the input array!" )
1694
+
1689
1695
new_values = take_nd (mapper ._values , indexer )
1690
1696
1691
1697
return new_values
0 commit comments