Skip to content

Commit cd57c9d

Browse files
Replace na_action by skipna
1 parent cfe54bd commit cd57c9d

File tree

13 files changed

+114
-55
lines changed

13 files changed

+114
-55
lines changed

pandas/core/algorithms.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,9 +1629,7 @@ def union_with_duplicates(
16291629

16301630

16311631
def map_array(
1632-
arr: ArrayLike,
1633-
mapper,
1634-
na_action: Literal["ignore"] | None = None,
1632+
arr: ArrayLike, mapper, skipna: bool = False
16351633
) -> np.ndarray | ExtensionArray | Index:
16361634
"""
16371635
Map values using an input mapping or function.
@@ -1640,8 +1638,8 @@ def map_array(
16401638
----------
16411639
mapper : function, dict, or Series
16421640
Mapping correspondence.
1643-
na_action : {None, 'ignore'}, default None
1644-
If 'ignore', propagate NA values, without passing them to the
1641+
skipna : bool, default False
1642+
If ``True``, propagate NA values, without passing them to the
16451643
mapping correspondence.
16461644
16471645
Returns
@@ -1653,10 +1651,6 @@ def map_array(
16531651
"""
16541652
from pandas import Index
16551653

1656-
if na_action not in (None, "ignore"):
1657-
msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
1658-
raise ValueError(msg)
1659-
16601654
# we can fastpath dict/Series to an efficient map
16611655
# as we know that we are not going to have to yield
16621656
# python types
@@ -1690,7 +1684,7 @@ def map_array(
16901684
mapper = Series(mapper)
16911685

16921686
if isinstance(mapper, ABCSeries):
1693-
if na_action == "ignore":
1687+
if skipna:
16941688
mapper = mapper[mapper.index.notna()]
16951689

16961690
# Since values were input this means we came from either
@@ -1705,7 +1699,7 @@ def map_array(
17051699

17061700
# we must convert to python types
17071701
values = arr.astype(object, copy=False)
1708-
if na_action is None:
1702+
if not skipna:
17091703
return lib.map_infer(values, mapper)
17101704
else:
17111705
return lib.map_infer_mask(values, mapper, mask=isna(values).view(np.uint8))

pandas/core/apply.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def map(
126126
executor the user wants to use.
127127
skip_na : bool
128128
Whether the function should be called for missing values or not.
129-
This is specified by the pandas user as ``map(na_action=None)``
130-
or ``map(na_action='ignore')``.
129+
This is specified by the pandas user as ``map(skipna=)``.
131130
"""
132131

133132
@staticmethod

pandas/core/arrays/arrow/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,11 +1477,11 @@ def to_numpy(
14771477
result[~mask] = data[~mask]._pa_array.to_numpy()
14781478
return result
14791479

1480-
def map(self, mapper, na_action: Literal["ignore"] | None = None):
1480+
def map(self, mapper, skipna: bool = False):
14811481
if is_numeric_dtype(self.dtype):
1482-
return map_array(self.to_numpy(), mapper, na_action=na_action)
1482+
return map_array(self.to_numpy(), mapper, skipna=skipna)
14831483
else:
1484-
return super().map(mapper, na_action)
1484+
return super().map(mapper, skipna)
14851485

14861486
@doc(ExtensionArray.duplicated)
14871487
def duplicated(

pandas/core/arrays/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,17 +2541,17 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
25412541

25422542
return arraylike.default_array_ufunc(self, ufunc, method, *inputs, **kwargs)
25432543

2544-
def map(self, mapper, na_action: Literal["ignore"] | None = None):
2544+
def map(self, mapper, skipna: bool = False):
25452545
"""
25462546
Map values using an input mapping or function.
25472547
25482548
Parameters
25492549
----------
25502550
mapper : function, dict, or Series
25512551
Mapping correspondence.
2552-
na_action : {None, 'ignore'}, default None
2553-
If 'ignore', propagate NA values, without passing them to the
2554-
mapping correspondence. If 'ignore' is not supported, a
2552+
skipna : bool, default False
2553+
If ``True``, propagate NA values, without passing them to the
2554+
mapping correspondence. If ``True`` is not supported, a
25552555
``NotImplementedError`` should be raised.
25562556
25572557
Returns
@@ -2561,7 +2561,7 @@ def map(self, mapper, na_action: Literal["ignore"] | None = None):
25612561
If the function returns a tuple with more than one element
25622562
a MultiIndex will be returned.
25632563
"""
2564-
return map_array(self, mapper, na_action=na_action)
2564+
return map_array(self, mapper, skipna=skipna)
25652565

25662566
# ------------------------------------------------------------------------
25672567
# GroupBy Methods

pandas/core/arrays/categorical.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ def remove_unused_categories(self) -> Self:
14971497
def map(
14981498
self,
14991499
mapper,
1500-
na_action: Literal["ignore"] | None = None,
1500+
skipna: bool = False,
15011501
):
15021502
"""
15031503
Map categories using an input mapping or function.
@@ -1515,8 +1515,8 @@ def map(
15151515
----------
15161516
mapper : function, dict, or Series
15171517
Mapping correspondence.
1518-
na_action : {None, 'ignore'}, default None
1519-
If 'ignore', propagate NaN values, without passing them to the
1518+
skipna : bool, default False
1519+
If ``True``, propagate NA values, without passing them to the
15201520
mapping correspondence.
15211521
15221522
Returns
@@ -1541,10 +1541,10 @@ def map(
15411541
>>> cat
15421542
['a', 'b', 'c']
15431543
Categories (3, object): ['a', 'b', 'c']
1544-
>>> cat.map(lambda x: x.upper(), na_action=None)
1544+
>>> cat.map(lambda x: x.upper(), skipna=False)
15451545
['A', 'B', 'C']
15461546
Categories (3, object): ['A', 'B', 'C']
1547-
>>> cat.map({"a": "first", "b": "second", "c": "third"}, na_action=None)
1547+
>>> cat.map({"a": "first", "b": "second", "c": "third"}, skipna=False)
15481548
['first', 'second', 'third']
15491549
Categories (3, object): ['first', 'second', 'third']
15501550
@@ -1555,19 +1555,19 @@ def map(
15551555
>>> cat
15561556
['a', 'b', 'c']
15571557
Categories (3, object): ['a' < 'b' < 'c']
1558-
>>> cat.map({"a": 3, "b": 2, "c": 1}, na_action=None)
1558+
>>> cat.map({"a": 3, "b": 2, "c": 1}, skipna=False)
15591559
[3, 2, 1]
15601560
Categories (3, int64): [3 < 2 < 1]
15611561
15621562
If the mapping is not one-to-one an :class:`~pandas.Index` is returned:
15631563
1564-
>>> cat.map({"a": "first", "b": "second", "c": "first"}, na_action=None)
1564+
>>> cat.map({"a": "first", "b": "second", "c": "first"}, skipna=False)
15651565
Index(['first', 'second', 'first'], dtype='object')
15661566
15671567
If a `dict` is used, all unmapped categories are mapped to `NaN` and
15681568
the result is an :class:`~pandas.Index`:
15691569
1570-
>>> cat.map({"a": "first", "b": "second"}, na_action=None)
1570+
>>> cat.map({"a": "first", "b": "second"}, skipna=False)
15711571
Index(['first', 'second', nan], dtype='object')
15721572
"""
15731573
assert callable(mapper) or is_dict_like(mapper)
@@ -1577,7 +1577,7 @@ def map(
15771577
has_nans = np.any(self._codes == -1)
15781578

15791579
na_val = np.nan
1580-
if na_action is None and has_nans:
1580+
if not skipna and has_nans:
15811581
na_val = mapper(np.nan) if callable(mapper) else mapper.get(np.nan, np.nan)
15821582

15831583
if new_categories.is_unique and not new_categories.hasnans and na_val is np.nan:

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,10 @@ def _unbox(self, other) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarra
743743
# pandas assumes they're there.
744744

745745
@ravel_compat
746-
def map(self, mapper, na_action: Literal["ignore"] | None = None):
746+
def map(self, mapper, skipna: bool = False):
747747
from pandas import Index
748748

749-
result = map_array(self, mapper, na_action=na_action)
749+
result = map_array(self, mapper, skipna=skipna)
750750
result = Index(result)
751751

752752
if isinstance(result, ABCMultiIndex):

pandas/core/arrays/masked.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ def max(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
13241324
)
13251325
return self._wrap_reduction_result("max", result, skipna=skipna, axis=axis)
13261326

1327-
def map(self, mapper, na_action: Literal["ignore"] | None = None):
1328-
return map_array(self.to_numpy(), mapper, na_action=na_action)
1327+
def map(self, mapper, skipna: bool = False):
1328+
return map_array(self.to_numpy(), mapper, skipna=skipna)
13291329

13301330
@overload
13311331
def any(

pandas/core/arrays/sparse/array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,16 +1309,16 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
13091309

13101310
return self._simple_new(sp_values, self.sp_index, dtype)
13111311

1312-
def map(self, mapper, na_action: Literal["ignore"] | None = None) -> Self:
1312+
def map(self, mapper, skipna: bool = False) -> Self:
13131313
"""
13141314
Map categories using an input mapping or function.
13151315
13161316
Parameters
13171317
----------
13181318
mapper : dict, Series, callable
13191319
The correspondence from old values to new.
1320-
na_action : {None, 'ignore'}, default None
1321-
If 'ignore', propagate NA values, without passing them to the
1320+
skipna : bool, default False
1321+
If ``True``, propagate NA values, without passing them to the
13221322
mapping correspondence.
13231323
13241324
Returns
@@ -1353,7 +1353,7 @@ def map(self, mapper, na_action: Literal["ignore"] | None = None) -> Self:
13531353

13541354
fill_val = self.fill_value
13551355

1356-
if na_action is None or notna(fill_val):
1356+
if not skipna or notna(fill_val):
13571357
fill_val = mapper.get(fill_val, fill_val) if is_map else mapper(fill_val)
13581358

13591359
def func(sp_val):

pandas/core/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ def hasnans(self) -> bool:
945945
return bool(isna(self).any()) # type: ignore[union-attr]
946946

947947
@final
948-
def _map_values(self, mapper, na_action=None):
948+
def _map_values(self, mapper, skipna=False):
949949
"""
950950
An internal function that maps values using the input
951951
correspondence (which can be a dict, Series, or function).
@@ -954,8 +954,8 @@ def _map_values(self, mapper, na_action=None):
954954
----------
955955
mapper : function, dict, or Series
956956
The input correspondence object
957-
na_action : {None, 'ignore'}
958-
If 'ignore', propagate NA values, without passing them to the
957+
skipna : bool, default False
958+
If ``True``, propagate NA values, without passing them to the
959959
mapping function
960960
961961
Returns
@@ -968,9 +968,9 @@ def _map_values(self, mapper, na_action=None):
968968
arr = self._values
969969

970970
if isinstance(arr, ExtensionArray):
971-
return arr.map(mapper, na_action=na_action)
971+
return arr.map(mapper, skipna=skipna)
972972

973-
return algorithms.map_array(arr, mapper, na_action=na_action)
973+
return algorithms.map_array(arr, mapper, skipna=skipna)
974974

975975
def value_counts(
976976
self,

pandas/core/frame.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10641,7 +10641,11 @@ def apply(
1064110641
raise ValueError(f"Unknown engine {engine}")
1064210642

1064310643
def map(
10644-
self, func: PythonFuncType, na_action: Literal["ignore"] | None = None, **kwargs
10644+
self,
10645+
func: PythonFuncType,
10646+
skipna: bool = False,
10647+
na_action: Literal["ignore"] | None = None,
10648+
**kwargs,
1064510649
) -> DataFrame:
1064610650
"""
1064710651
Apply a function to a Dataframe elementwise.
@@ -10657,6 +10661,8 @@ def map(
1065710661
----------
1065810662
func : callable
1065910663
Python function, returns a single value from a single value.
10664+
skipna : bool = False
10665+
If ``True``, propagate missing values without passing them to ``func``.
1066010666
na_action : {None, 'ignore'}, default None
1066110667
If 'ignore', propagate NaN values, without passing them to func.
1066210668
**kwargs
@@ -10691,7 +10697,7 @@ def map(
1069110697
1069210698
>>> df_copy = df.copy()
1069310699
>>> df_copy.iloc[0, 0] = pd.NA
10694-
>>> df_copy.map(lambda x: len(str(x)), na_action="ignore")
10700+
>>> df_copy.map(lambda x: len(str(x)), skipna=True)
1069510701
0 1
1069610702
0 NaN 4
1069710703
1 5.0 5
@@ -10719,16 +10725,22 @@ def map(
1071910725
0 1.000000 4.494400
1072010726
1 11.262736 20.857489
1072110727
"""
10722-
if na_action not in {"ignore", None}:
10723-
raise ValueError(f"na_action must be 'ignore' or None. Got {na_action!r}")
10728+
if na_action == "ignore":
10729+
warnings.warn(
10730+
"The ``na_action`` parameter has been deprecated and it will be "
10731+
"removed in a future version of pandas. Use ``skipna`` instead.",
10732+
FutureWarning,
10733+
stacklevel=find_stack_level(),
10734+
)
10735+
skipna = True
1072410736

1072510737
if self.empty:
1072610738
return self.copy()
1072710739

1072810740
func = functools.partial(func, **kwargs)
1072910741

1073010742
def infer(x):
10731-
return x._map_values(func, na_action=na_action)
10743+
return x._map_values(func, skipna=skipna)
1073210744

1073310745
return self.apply(infer).__finalize__(self, "map")
1073410746

0 commit comments

Comments
 (0)