Skip to content

Commit

Permalink
CLN: Change Categorical.map(na_action=) default to None (#58625)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed May 8, 2024
1 parent ad06bbb commit 882b228
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 37 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Removal of prior version deprecations/changes
- Removed the "closed" and "unit" keywords in :meth:`TimedeltaIndex.__new__` (:issue:`52628`, :issue:`55499`)
- All arguments in :meth:`Index.sort_values` are now keyword only (:issue:`56493`)
- All arguments in :meth:`Series.to_dict` are now keyword only (:issue:`56493`)
- Changed the default value of ``na_action`` in :meth:`Categorical.map` to ``None`` (:issue:`51645`)
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
- Enforce deprecation in :func:`testing.assert_series_equal` and :func:`testing.assert_frame_equal` with object dtype and mismatched null-like values, which are now considered not-equal (:issue:`18463`)
- Enforced deprecation ``all`` and ``any`` reductions with ``datetime64``, :class:`DatetimeTZDtype`, and :class:`PeriodDtype` dtypes (:issue:`58029`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ def to_numpy(
result[~mask] = data[~mask]._pa_array.to_numpy()
return result

def map(self, mapper, na_action=None):
def map(self, mapper, na_action: Literal["ignore"] | None = None):
if is_numeric_dtype(self.dtype):
return map_array(self.to_numpy(), mapper, na_action=na_action)
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,7 +2270,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):

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

def map(self, mapper, na_action=None):
def map(self, mapper, na_action: Literal["ignore"] | None = None):
"""
Map values using an input mapping or function.
Expand Down
20 changes: 2 additions & 18 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ def remove_unused_categories(self) -> Self:
def map(
self,
mapper,
na_action: Literal["ignore"] | None | lib.NoDefault = lib.no_default,
na_action: Literal["ignore"] | None = None,
):
"""
Map categories using an input mapping or function.
Expand All @@ -1501,15 +1501,10 @@ def map(
----------
mapper : function, dict, or Series
Mapping correspondence.
na_action : {None, 'ignore'}, default 'ignore'
na_action : {None, 'ignore'}, default None
If 'ignore', propagate NaN values, without passing them to the
mapping correspondence.
.. deprecated:: 2.1.0
The default value of 'ignore' has been deprecated and will be changed to
None in the future.
Returns
-------
pandas.Categorical or pandas.Index
Expand Down Expand Up @@ -1561,17 +1556,6 @@ def map(
>>> cat.map({"a": "first", "b": "second"}, na_action=None)
Index(['first', 'second', nan], dtype='object')
"""
if na_action is lib.no_default:
warnings.warn(
"The default value of 'ignore' for the `na_action` parameter in "
"pandas.Categorical.map is deprecated and will be "
"changed to 'None' in a future version. Please set na_action to the "
"desired value to avoid seeing this warning",
FutureWarning,
stacklevel=find_stack_level(),
)
na_action = "ignore"

assert callable(mapper) or is_dict_like(mapper)

new_categories = self.categories.map(mapper)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def _unbox(self, other) -> np.int64 | np.datetime64 | np.timedelta64 | np.ndarra
# pandas assumes they're there.

@ravel_compat
def map(self, mapper, na_action=None):
def map(self, mapper, na_action: Literal["ignore"] | None = None):
from pandas import Index

result = map_array(self, mapper, na_action=na_action)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def max(self, *, skipna: bool = True, axis: AxisInt | None = 0, **kwargs):
)
return self._wrap_reduction_result("max", result, skipna=skipna, axis=axis)

def map(self, mapper, na_action=None):
def map(self, mapper, na_action: Literal["ignore"] | None = None):
return map_array(self.to_numpy(), mapper, na_action=na_action)

@overload
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):

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

def map(self, mapper, na_action=None) -> Self:
def map(self, mapper, na_action: Literal["ignore"] | None = None) -> Self:
"""
Map categories using an input mapping or function.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10369,7 +10369,7 @@ def apply(
return op.apply().__finalize__(self, method="apply")

def map(
self, func: PythonFuncType, na_action: str | None = None, **kwargs
self, func: PythonFuncType, na_action: Literal["ignore"] | None = None, **kwargs
) -> DataFrame:
"""
Apply a function to a Dataframe elementwise.
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/arrays/categorical/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,3 @@ def test_map_with_dict_or_series(na_action):
result = cat.map(mapper, na_action=na_action)
# Order of categories in result can be different
tm.assert_categorical_equal(result, expected)


def test_map_na_action_no_default_deprecated():
# GH51645
cat = Categorical(["a", "b", "c"])
msg = (
"The default value of 'ignore' for the `na_action` parameter in "
"pandas.Categorical.map is deprecated and will be "
"changed to 'None' in a future version. Please set na_action to the "
"desired value to avoid seeing this warning"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
cat.map(lambda x: x)

0 comments on commit 882b228

Please sign in to comment.