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