Skip to content

CLN: some cleanups in Series.apply & related #52786

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

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 16 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from pandas._config import option_context

from pandas._libs import lib
from pandas._typing import (
AggFuncType,
AggFuncTypeBase,
Expand Down Expand Up @@ -110,6 +111,7 @@ def __init__(
func,
raw: bool,
result_type: str | None,
*,
args,
kwargs,
) -> None:
Expand Down Expand Up @@ -1037,10 +1039,21 @@ def __init__(
self,
obj: Series,
func: AggFuncType,
convert_dtype: bool,
*,
convert_dtype: bool | lib.NoDefault = lib.no_default,
args,
kwargs,
) -> None:
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(),
)
self.convert_dtype = convert_dtype

super().__init__(
Expand Down Expand Up @@ -1143,6 +1156,7 @@ def __init__(
self,
obj: GroupBy[NDFrameT],
func: AggFuncType,
*,
args,
kwargs,
) -> None:
Expand Down Expand Up @@ -1172,6 +1186,7 @@ def __init__(
self,
obj: Resampler | BaseWindow,
func: AggFuncType,
*,
args,
kwargs,
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
func = maybe_mangle_lambdas(func)

op = GroupByApply(self, func, args, kwargs)
op = GroupByApply(self, func, args=args, kwargs=kwargs)
result = op.agg()
if not is_dict_like(func) and result is not None:
return result
Expand Down
20 changes: 5 additions & 15 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4365,7 +4365,7 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
if func is None:
func = dict(kwargs.items())

op = SeriesApply(self, func, convert_dtype=False, args=args, kwargs=kwargs)
op = SeriesApply(self, func, args=args, kwargs=kwargs)
result = op.agg()
return result

Expand All @@ -4381,9 +4381,7 @@ def transform(
) -> DataFrame | Series:
# Validate axis argument
self._get_axis_number(axis)
result = SeriesApply(
self, func=func, convert_dtype=True, args=args, kwargs=kwargs
).transform()
result = SeriesApply(self, func=func, args=args, kwargs=kwargs).transform()
return result

def apply(
Expand Down Expand Up @@ -4505,17 +4503,9 @@ 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()
return SeriesApply(
self, func, convert_dtype=convert_dtype, args=args, kwargs=kwargs
).apply()

def _reindex_indexer(
self,
Expand Down