Skip to content

Commit 27a3188

Browse files
committed
implement na_action='raise' to map_array()
1 parent bc24e84 commit 27a3188

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
@@ -1627,7 +1627,7 @@ def union_with_duplicates(
16271627
def map_array(
16281628
arr: ArrayLike,
16291629
mapper,
1630-
na_action: Literal["ignore"] | None = None,
1630+
na_action: Literal["ignore", "raise"] | None = None,
16311631
) -> np.ndarray | ExtensionArray | Index:
16321632
"""
16331633
Map values using an input mapping or function.
@@ -1636,9 +1636,10 @@ def map_array(
16361636
----------
16371637
mapper : function, dict, or Series
16381638
Mapping correspondence.
1639-
na_action : {None, 'ignore'}, default None
1639+
na_action : {None, 'ignore', 'raise'}, default None
16401640
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.
16421643
16431644
Returns
16441645
-------
@@ -1647,7 +1648,7 @@ def map_array(
16471648
If the function returns a tuple with more than one element
16481649
a MultiIndex will be returned.
16491650
"""
1650-
if na_action not in (None, "ignore"):
1651+
if na_action not in (None, "ignore", "raise"):
16511652
msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
16521653
raise ValueError(msg)
16531654

@@ -1686,6 +1687,11 @@ def map_array(
16861687
# Since values were input this means we came from either
16871688
# a dict or a series and mapper should be an index
16881689
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+
16891695
new_values = take_nd(mapper._values, indexer)
16901696

16911697
return new_values

0 commit comments

Comments
 (0)