Skip to content

Commit f7856e8

Browse files
committed
implement na_action='raise' to map_array()
1 parent d067e08 commit f7856e8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pandas/core/algorithms.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ def union_with_duplicates(
16211621
def map_array(
16221622
arr: ArrayLike,
16231623
mapper,
1624-
na_action: Literal["ignore"] | None = None,
1624+
na_action: Literal["ignore", "raise"] | None = None,
16251625
) -> np.ndarray | ExtensionArray | Index:
16261626
"""
16271627
Map values using an input mapping or function.
@@ -1630,9 +1630,10 @@ def map_array(
16301630
----------
16311631
mapper : function, dict, or Series
16321632
Mapping correspondence.
1633-
na_action : {None, 'ignore'}, default None
1633+
na_action : {None, 'ignore', 'raise'}, default None
16341634
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.
16361637
16371638
Returns
16381639
-------
@@ -1641,7 +1642,7 @@ def map_array(
16411642
If the function returns a tuple with more than one element
16421643
a MultiIndex will be returned.
16431644
"""
1644-
if na_action not in (None, "ignore"):
1645+
if na_action not in (None, "ignore", "raise"):
16451646
msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
16461647
raise ValueError(msg)
16471648

@@ -1680,6 +1681,11 @@ def map_array(
16801681
# Since values were input this means we came from either
16811682
# a dict or a series and mapper should be an index
16821683
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+
16831689
new_values = take_nd(mapper._values, indexer)
16841690

16851691
return new_values

0 commit comments

Comments
 (0)