Skip to content

Commit

Permalink
DEPR: Deprecate the convert_dtype param in Series.Apply (pandas-dev#5…
Browse files Browse the repository at this point in the history
…2257)

* 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]>
  • Loading branch information
topper-123 and mroeschke committed Apr 1, 2023
1 parent c8497cb commit 16da7b2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
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 @@ -126,6 +126,7 @@ Deprecations
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
- 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`.
This pattern was very slow and it's recommended to use alternative methods to archive the same goal (:issue:`52116`)
- 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 @@ -2808,12 +2808,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 @@ -2856,12 +2853,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
16 changes: 15 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4405,7 +4405,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 @@ -4428,6 +4428,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 ``convert_dtype=False``.
args : tuple
Positional arguments passed to func after the series value.
**kwargs
Expand Down Expand Up @@ -4517,6 +4521,16 @@ 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. Do ``ser.astype(object).apply()`` "
"instead if you want ``convert_dtype=False``.",
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

0 comments on commit 16da7b2

Please sign in to comment.