Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: Deprecate the convert_dtype param in Series.Apply #52257

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Deprecations
- Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`)
- 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`)
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
-

.. ---------------------------------------------------------------------------
Expand Down
18 changes: 6 additions & 12 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2797,12 +2797,9 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr
result[i] = val

if convert:
return maybe_convert_objects(result,
try_float=False,
convert_datetime=False,
convert_timedelta=False)

return result
return maybe_convert_objects(result)
else:
return result


@cython.boundscheck(False)
Expand Down Expand Up @@ -2845,12 +2842,9 @@ def map_infer(
result[i] = val

if convert:
return maybe_convert_objects(result,
try_float=False,
convert_datetime=False,
convert_timedelta=False)

return result
return maybe_convert_objects(result)
else:
return result


def to_object_array(rows: object, min_width: int = 0) -> ndarray:
Expand Down
15 changes: 14 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4387,7 +4387,7 @@ def transform(
def apply(
self,
func: AggFuncType,
convert_dtype: bool = True,
convert_dtype: bool | lib.NoDefault = lib.no_default,
args: tuple[Any, ...] = (),
**kwargs,
) -> DataFrame | Series:
Expand All @@ -4405,6 +4405,10 @@ def apply(
Try to find better dtype for elementwise function results. If
False, leave as dtype=object. Note that the dtype is always
preserved for some extension array dtypes, such as Categorical.

.. deprecated:: 2.1.0
The convert_dtype has been deprecated. Do ``ser.astype(object).apply()``
instead if you want this functionality.
args : tuple
Positional arguments passed to func after the series value.
**kwargs
Expand Down Expand Up @@ -4494,6 +4498,15 @@ def apply(
Helsinki 2.484907
dtype: float64
"""
if convert_dtype is lib.no_default:
convert_dtype = True
else:
warnings.warn(
"the convert_dtype parameter is deprecated and will be removed in a "
"future version.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to mention the alternative here if convert_dtype=False was specified

FutureWarning,
stacklevel=find_stack_level(),
)
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

def _reduce(
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ def read(
# Decode strings
for col, typ in zip(data, self._typlist):
if type(typ) is int:
data[col] = data[col].apply(self._decode, convert_dtype=True)
data[col] = data[col].apply(self._decode)

data = self._insert_strls(data)

Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ def f(x):
tm.assert_series_equal(result, expected)


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

def f(x):
def func(x):
return x if x > 0 else np.nan

result = s.apply(f, convert_dtype=False)
assert result.dtype == object
with tm.assert_produces_warning(FutureWarning):
ser.apply(func, convert_dtype=convert_dtype)


def test_apply_args():
Expand Down