Skip to content

REF: Move Series.apply/agg into apply #39061

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
Jan 9, 2021
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
196 changes: 151 additions & 45 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pandas._config import option_context

from pandas._libs import lib
from pandas._typing import (
AggFuncType,
AggFuncTypeBase,
Expand All @@ -26,7 +27,10 @@
from pandas.core.dtypes.generic import ABCSeries

from pandas.core.aggregation import agg_dict_like, agg_list_like
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.construction import (
array as pd_array,
create_series_with_explicit_dtype,
)

if TYPE_CHECKING:
from pandas import DataFrame, Index, Series
Expand Down Expand Up @@ -63,12 +67,30 @@ def frame_apply(
)


def series_apply(
obj: Series,
how: str,
func: AggFuncType,
convert_dtype: bool = True,
args=None,
kwds=None,
) -> SeriesApply:
return SeriesApply(
obj,
how,
func,
convert_dtype,
args,
kwds,
)


class Apply(metaclass=abc.ABCMeta):
axis: int

def __init__(
self,
obj: DataFrame,
obj: FrameOrSeriesUnion,
how: str,
func,
raw: bool,
Expand Down Expand Up @@ -110,12 +132,62 @@ def f(x):
def index(self) -> Index:
return self.obj.index

@abc.abstractmethod
def get_result(self):
if self.how == "apply":
return self.apply()
else:
return self.agg()

@abc.abstractmethod
def apply(self) -> FrameOrSeriesUnion:
pass

def agg(self) -> Tuple[Optional[FrameOrSeriesUnion], Optional[bool]]:
"""
Provide an implementation for the aggregators.

Returns
-------
tuple of result, how.

Notes
-----
how can be a string describe the required post-processing, or
None if not required.
"""
obj = self.obj
arg = self.f
args = self.args
kwargs = self.kwds

_axis = kwargs.pop("_axis", None)
if _axis is None:
_axis = getattr(obj, "axis", 0)

if isinstance(arg, str):
return obj._try_aggregate_string_function(arg, *args, **kwargs), None
elif is_dict_like(arg):
arg = cast(AggFuncTypeDict, arg)
return agg_dict_like(obj, arg, _axis), True
elif is_list_like(arg):
# we require a list, but not a 'str'
arg = cast(List[AggFuncTypeBase], arg)
return agg_list_like(obj, arg, _axis=_axis), None
else:
result = None

if callable(arg):
f = obj._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(obj, f)(), None

# caller can react
return result, True


class FrameApply(Apply):
obj: DataFrame

# ---------------------------------------------------------------
# Abstract Methods

Expand Down Expand Up @@ -168,48 +240,6 @@ def get_result(self):
else:
return self.agg()

def agg(self) -> Tuple[Optional[FrameOrSeriesUnion], Optional[bool]]:
"""
Provide an implementation for the aggregators.

Returns
-------
tuple of result, how.

Notes
-----
how can be a string describe the required post-processing, or
None if not required.
"""
obj = self.obj
arg = self.f
args = self.args
kwargs = self.kwds

_axis = kwargs.pop("_axis", None)
if _axis is None:
_axis = getattr(obj, "axis", 0)

if isinstance(arg, str):
return obj._try_aggregate_string_function(arg, *args, **kwargs), None
elif is_dict_like(arg):
arg = cast(AggFuncTypeDict, arg)
return agg_dict_like(obj, arg, _axis), True
elif is_list_like(arg):
# we require a list, but not a 'str'
arg = cast(List[AggFuncTypeBase], arg)
return agg_list_like(obj, arg, _axis=_axis), None
else:
result = None

if callable(arg):
f = obj._get_cython_func(arg)
if f and not args and not kwargs:
return getattr(obj, f)(), None

# caller can react
return result, True

def apply(self) -> FrameOrSeriesUnion:
""" compute the results """
# dispatch to agg
Expand Down Expand Up @@ -531,3 +561,79 @@ def infer_to_same_shape(self, results: ResType, res_index: Index) -> DataFrame:
result = result.infer_objects()

return result


class SeriesApply(Apply):
obj: Series
axis = 0

def __init__(
self,
obj: Series,
how: str,
func: AggFuncType,
convert_dtype: bool,
args,
kwds,
):
self.convert_dtype = convert_dtype

super().__init__(
obj,
how,
func,
raw=False,
result_type=None,
args=args,
kwds=kwds,
)

def apply(self) -> FrameOrSeriesUnion:
obj = self.obj
func = self.f
args = self.args
kwds = self.kwds

if len(obj) == 0:
return self.apply_empty_result()

# dispatch to agg
if isinstance(func, (list, dict)):
return obj.aggregate(func, *args, **kwds)

# if we are a string, try to dispatch
if isinstance(func, str):
return obj._try_aggregate_string_function(func, *args, **kwds)

return self.apply_standard()

def apply_empty_result(self) -> Series:
obj = self.obj
return obj._constructor(dtype=obj.dtype, index=obj.index).__finalize__(
obj, method="apply"
)

def apply_standard(self) -> FrameOrSeriesUnion:
f = self.f
obj = self.obj

with np.errstate(all="ignore"):
if isinstance(f, np.ufunc):
return f(obj)

# row-wise access
if is_extension_array_dtype(obj.dtype) and hasattr(obj._values, "map"):
# GH#23179 some EAs do not have `map`
mapped = obj._values.map(f)
else:
values = obj.astype(object)._values
mapped = lib.map_infer(values, f, convert=self.convert_dtype)

if len(mapped) and isinstance(mapped[0], ABCSeries):
# GH 25959 use pd.array instead of tolist
# so extension arrays can be used
return obj._constructor_expanddim(pd_array(mapped), index=obj.index)
else:
return obj._constructor(mapped, index=obj.index).__finalize__(
obj, method="apply"
)
51 changes: 6 additions & 45 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@

from pandas.core import algorithms, base, generic, missing, nanops, ops
from pandas.core.accessor import CachedAccessor
from pandas.core.aggregation import aggregate, transform
from pandas.core.aggregation import transform
from pandas.core.apply import series_apply
from pandas.core.arrays import ExtensionArray
from pandas.core.arrays.categorical import CategoricalAccessor
from pandas.core.arrays.sparse import SparseAccessor
import pandas.core.common as com
from pandas.core.construction import (
array as pd_array,
create_series_with_explicit_dtype,
extract_array,
is_empty_data,
Expand Down Expand Up @@ -3944,7 +3944,8 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):
if func is None:
func = dict(kwargs.items())

result, how = aggregate(self, func, *args, **kwargs)
op = series_apply(self, "agg", func, args=args, kwds=kwargs)
result, how = op.get_result()
if result is None:

# we can be called from an inner function which
Expand Down Expand Up @@ -4076,48 +4077,8 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
Helsinki 2.484907
dtype: float64
"""
if len(self) == 0:
return self._constructor(dtype=self.dtype, index=self.index).__finalize__(
self, method="apply"
)

# dispatch to agg
if isinstance(func, (list, dict)):
return self.aggregate(func, *args, **kwds)

# if we are a string, try to dispatch
if isinstance(func, str):
return self._try_aggregate_string_function(func, *args, **kwds)

# handle ufuncs and lambdas
if kwds or args and not isinstance(func, np.ufunc):

def f(x):
return func(x, *args, **kwds)

else:
f = func

with np.errstate(all="ignore"):
if isinstance(f, np.ufunc):
return f(self)

# row-wise access
if is_extension_array_dtype(self.dtype) and hasattr(self._values, "map"):
# GH#23179 some EAs do not have `map`
mapped = self._values.map(f)
else:
values = self.astype(object)._values
mapped = lib.map_infer(values, f, convert=convert_dtype)

if len(mapped) and isinstance(mapped[0], Series):
# GH 25959 use pd.array instead of tolist
# so extension arrays can be used
return self._constructor_expanddim(pd_array(mapped), index=self.index)
else:
return self._constructor(mapped, index=self.index).__finalize__(
self, method="apply"
)
op = series_apply(self, "apply", func, convert_dtype, args, kwds)
return op.get_result()

def _reduce(
self,
Expand Down