Skip to content

Commit 16da7b2

Browse files
topper-123mroeschke
andcommitted
DEPR: Deprecate the convert_dtype param in Series.Apply (pandas-dev#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 c8497cb commit 16da7b2

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
@@ -126,6 +126,7 @@ Deprecations
126126
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
127127
- Deprecated making :meth:`Series.apply` return a :class:`DataFrame` when the passed-in callable returns a :class:`Series` object. In the future this will return a :class:`Series`.
128128
This pattern was very slow and it's recommended to use alternative methods to archive the same goal (:issue:`52116`)
129+
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
129130
-
130131

131132
.. ---------------------------------------------------------------------------

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:
@@ -4428,6 +4428,10 @@ def apply(
44284428
Try to find better dtype for elementwise function results. If
44294429
False, leave as dtype=object. Note that the dtype is always
44304430
preserved for some extension array dtypes, such as Categorical.
4431+
4432+
.. deprecated:: 2.1.0
4433+
The convert_dtype has been deprecated. Do ``ser.astype(object).apply()``
4434+
instead if you want ``convert_dtype=False``.
44314435
args : tuple
44324436
Positional arguments passed to func after the series value.
44334437
**kwargs
@@ -4517,6 +4521,16 @@ def apply(
45174521
Helsinki 2.484907
45184522
dtype: float64
45194523
"""
4524+
if convert_dtype is lib.no_default:
4525+
convert_dtype = True
4526+
else:
4527+
warnings.warn(
4528+
"the convert_dtype parameter is deprecated and will be removed in a "
4529+
"future version. Do ``ser.astype(object).apply()`` "
4530+
"instead if you want ``convert_dtype=False``.",
4531+
FutureWarning,
4532+
stacklevel=find_stack_level(),
4533+
)
45204534
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
45214535

45224536
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)