Skip to content

Commit 632a41f

Browse files
authored
Remove convert_dtype from Series.apply (#57369)
1 parent 87b1e42 commit 632a41f

File tree

6 files changed

+7
-52
lines changed

6 files changed

+7
-52
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Removal of prior version deprecations/changes
120120
- Removed ``StataReader.close`` (:issue:`49228`)
121121
- Removed ``axis`` argument from :meth:`DataFrame.groupby`, :meth:`Series.groupby`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.resample`, and :meth:`Series.resample` (:issue:`51203`)
122122
- Removed ``axis`` argument from all groupby operations (:issue:`50405`)
123+
- Removed ``convert_dtype`` from :meth:`Series.apply` (:issue:`52257`)
123124
- Removed ``pandas.api.types.is_interval`` and ``pandas.api.types.is_period``, use ``isinstance(obj, pd.Interval)`` and ``isinstance(obj, pd.Period)`` instead (:issue:`55264`)
124125
- Removed ``pandas.io.sql.execute`` (:issue:`50185`)
125126
- Removed ``pandas.value_counts``, use :meth:`Series.value_counts` instead (:issue:`53493`)
@@ -134,6 +135,7 @@ Removal of prior version deprecations/changes
134135
- Removed unused arguments ``*args`` and ``**kwargs`` in :class:`Resampler` methods (:issue:`50977`)
135136
- Unrecognized timezones when parsing strings to datetimes now raises a ``ValueError`` (:issue:`51477`)
136137

138+
137139
.. ---------------------------------------------------------------------------
138140
.. _whatsnew_300.performance:
139141

pandas/core/algorithms.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,6 @@ def map_array(
16351635
arr: ArrayLike,
16361636
mapper,
16371637
na_action: Literal["ignore"] | None = None,
1638-
convert: bool = True,
16391638
) -> np.ndarray | ExtensionArray | Index:
16401639
"""
16411640
Map values using an input mapping or function.
@@ -1647,9 +1646,6 @@ def map_array(
16471646
na_action : {None, 'ignore'}, default None
16481647
If 'ignore', propagate NA values, without passing them to the
16491648
mapping correspondence.
1650-
convert : bool, default True
1651-
Try to find better dtype for elementwise function results. If
1652-
False, leave as dtype=object.
16531649
16541650
Returns
16551651
-------
@@ -1707,8 +1703,6 @@ def map_array(
17071703
# we must convert to python types
17081704
values = arr.astype(object, copy=False)
17091705
if na_action is None:
1710-
return lib.map_infer(values, mapper, convert=convert)
1706+
return lib.map_infer(values, mapper)
17111707
else:
1712-
return lib.map_infer_mask(
1713-
values, mapper, mask=isna(values).view(np.uint8), convert=convert
1714-
)
1708+
return lib.map_infer_mask(values, mapper, mask=isna(values).view(np.uint8))

pandas/core/apply.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import numpy as np
1818

19-
from pandas._libs import lib
2019
from pandas._libs.internals import BlockValuesRefs
2120
from pandas._typing import (
2221
AggFuncType,
@@ -1376,23 +1375,10 @@ def __init__(
13761375
obj: Series,
13771376
func: AggFuncType,
13781377
*,
1379-
convert_dtype: bool | lib.NoDefault = lib.no_default,
13801378
by_row: Literal[False, "compat", "_compat"] = "compat",
13811379
args,
13821380
kwargs,
13831381
) -> None:
1384-
if convert_dtype is lib.no_default:
1385-
convert_dtype = True
1386-
else:
1387-
warnings.warn(
1388-
"the convert_dtype parameter is deprecated and will be removed in a "
1389-
"future version. Do ``ser.astype(object).apply()`` "
1390-
"instead if you want ``convert_dtype=False``.",
1391-
FutureWarning,
1392-
stacklevel=find_stack_level(),
1393-
)
1394-
self.convert_dtype = convert_dtype
1395-
13961382
super().__init__(
13971383
obj,
13981384
func,
@@ -1486,9 +1472,7 @@ def curried(x):
14861472
# TODO: remove the `na_action="ignore"` when that default has been changed in
14871473
# Categorical (GH51645).
14881474
action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
1489-
mapped = obj._map_values(
1490-
mapper=curried, na_action=action, convert=self.convert_dtype
1491-
)
1475+
mapped = obj._map_values(mapper=curried, na_action=action)
14921476

14931477
if len(mapped) and isinstance(mapped[0], ABCSeries):
14941478
# GH#43986 Need to do list(mapped) in order to get treated as nested

pandas/core/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ def hasnans(self) -> bool:
896896
return bool(isna(self).any()) # type: ignore[union-attr]
897897

898898
@final
899-
def _map_values(self, mapper, na_action=None, convert: bool = True):
899+
def _map_values(self, mapper, na_action=None):
900900
"""
901901
An internal function that maps values using the input
902902
correspondence (which can be a dict, Series, or function).
@@ -908,10 +908,6 @@ def _map_values(self, mapper, na_action=None, convert: bool = True):
908908
na_action : {None, 'ignore'}
909909
If 'ignore', propagate NA values, without passing them to the
910910
mapping function
911-
convert : bool, default True
912-
Try to find better dtype for elementwise function results. If
913-
False, leave as dtype=object. Note that the dtype is always
914-
preserved for some extension array dtypes, such as Categorical.
915911
916912
Returns
917913
-------
@@ -925,7 +921,7 @@ def _map_values(self, mapper, na_action=None, convert: bool = True):
925921
if isinstance(arr, ExtensionArray):
926922
return arr.map(mapper, na_action=na_action)
927923

928-
return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)
924+
return algorithms.map_array(arr, mapper, na_action=na_action)
929925

930926
@final
931927
def value_counts(

pandas/core/series.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,7 +4481,6 @@ def transform(
44814481
def apply(
44824482
self,
44834483
func: AggFuncType,
4484-
convert_dtype: bool | lib.NoDefault = lib.no_default,
44854484
args: tuple[Any, ...] = (),
44864485
*,
44874486
by_row: Literal[False, "compat"] = "compat",
@@ -4497,14 +4496,6 @@ def apply(
44974496
----------
44984497
func : function
44994498
Python function or NumPy ufunc to apply.
4500-
convert_dtype : bool, default True
4501-
Try to find better dtype for elementwise function results. If
4502-
False, leave as dtype=object. Note that the dtype is always
4503-
preserved for some extension array dtypes, such as Categorical.
4504-
4505-
.. deprecated:: 2.1.0
4506-
``convert_dtype`` has been deprecated. Do ``ser.astype(object).apply()``
4507-
instead if you want ``convert_dtype=False``.
45084499
args : tuple
45094500
Positional arguments passed to func after the series value.
45104501
by_row : False or "compat", default "compat"
@@ -4608,7 +4599,6 @@ def apply(
46084599
return SeriesApply(
46094600
self,
46104601
func,
4611-
convert_dtype=convert_dtype,
46124602
by_row=by_row,
46134603
args=args,
46144604
kwargs=kwargs,

pandas/tests/apply/test_series_apply.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,6 @@ def f(x):
7575
tm.assert_series_equal(result, expected)
7676

7777

78-
@pytest.mark.parametrize("convert_dtype", [True, False])
79-
def test_apply_convert_dtype_deprecated(convert_dtype):
80-
ser = Series(np.random.default_rng(2).standard_normal(10))
81-
82-
def func(x):
83-
return x if x > 0 else np.nan
84-
85-
with tm.assert_produces_warning(FutureWarning):
86-
ser.apply(func, convert_dtype=convert_dtype, by_row="compat")
87-
88-
8978
def test_apply_args():
9079
s = Series(["foo,bar"])
9180

0 commit comments

Comments
 (0)