Skip to content

Commit 825b5c3

Browse files
DEPR: Deprecate the convert_dtype param in Series.Apply (#52257)
* DEPR: Deprecate param convert_dtype in Series.Apply * fix StataReader * fix issue * Update pandas/core/series.py Co-authored-by: Matthew Roeschke <[email protected]> * explain more in warning --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 37e823d commit 825b5c3

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Deprecations
124124
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)
125125
- Deprecated logical operations (``|``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``), wrap a sequence in a :class:`Series` or numpy array before operating instead (:issue:`51521`)
126126
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
127+
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
127128
-
128129

129130
.. ---------------------------------------------------------------------------

pandas/_libs/lib.pyx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,12 +2808,9 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr
28082808
result[i] = val
28092809

28102810
if convert:
2811-
return maybe_convert_objects(result,
2812-
try_float=False,
2813-
convert_datetime=False,
2814-
convert_timedelta=False)
2815-
2816-
return result
2811+
return maybe_convert_objects(result)
2812+
else:
2813+
return result
28172814

28182815

28192816
@cython.boundscheck(False)
@@ -2856,12 +2853,9 @@ def map_infer(
28562853
result[i] = val
28572854

28582855
if convert:
2859-
return maybe_convert_objects(result,
2860-
try_float=False,
2861-
convert_datetime=False,
2862-
convert_timedelta=False)
2863-
2864-
return result
2856+
return maybe_convert_objects(result)
2857+
else:
2858+
return result
28652859

28662860

28672861
def to_object_array(rows: object, min_width: int = 0) -> ndarray:

pandas/core/series.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4405,7 +4405,7 @@ def transform(
44054405
def apply(
44064406
self,
44074407
func: AggFuncType,
4408-
convert_dtype: bool = True,
4408+
convert_dtype: bool | lib.NoDefault = lib.no_default,
44094409
args: tuple[Any, ...] = (),
44104410
**kwargs,
44114411
) -> DataFrame | Series:
@@ -4423,6 +4423,10 @@ def apply(
44234423
Try to find better dtype for elementwise function results. If
44244424
False, leave as dtype=object. Note that the dtype is always
44254425
preserved for some extension array dtypes, such as Categorical.
4426+
4427+
.. deprecated:: 2.1.0
4428+
The convert_dtype has been deprecated. Do ``ser.astype(object).apply()``
4429+
instead if you want ``convert_dtype=False``.
44264430
args : tuple
44274431
Positional arguments passed to func after the series value.
44284432
**kwargs
@@ -4512,6 +4516,16 @@ def apply(
45124516
Helsinki 2.484907
45134517
dtype: float64
45144518
"""
4519+
if convert_dtype is lib.no_default:
4520+
convert_dtype = True
4521+
else:
4522+
warnings.warn(
4523+
"the convert_dtype parameter is deprecated and will be removed in a "
4524+
"future version. Do ``ser.astype(object).apply()`` "
4525+
"instead if you want ``convert_dtype=False``.",
4526+
FutureWarning,
4527+
stacklevel=find_stack_level(),
4528+
)
45154529
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
45164530

45174531
def _reduce(

pandas/io/stata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ def read(
17801780
# Decode strings
17811781
for col, typ in zip(data, self._typlist):
17821782
if type(typ) is int:
1783-
data[col] = data[col].apply(self._decode, convert_dtype=True)
1783+
data[col] = data[col].apply(self._decode)
17841784

17851785
data = self._insert_strls(data)
17861786

pandas/tests/apply/test_series_apply.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ def f(x):
7474
tm.assert_series_equal(result, expected)
7575

7676

77-
def test_apply_dont_convert_dtype():
78-
s = Series(np.random.randn(10))
77+
@pytest.mark.parametrize("convert_dtype", [True, False])
78+
def test_apply_convert_dtype_deprecated(convert_dtype):
79+
ser = Series(np.random.randn(10))
7980

80-
def f(x):
81+
def func(x):
8182
return x if x > 0 else np.nan
8283

83-
result = s.apply(f, convert_dtype=False)
84-
assert result.dtype == object
84+
with tm.assert_produces_warning(FutureWarning):
85+
ser.apply(func, convert_dtype=convert_dtype)
8586

8687

8788
def test_apply_args():

0 commit comments

Comments
 (0)