Skip to content

REF: maybe_apply_* in core.apply #41224

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 2 commits into from
Apr 30, 2021
Merged
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
30 changes: 12 additions & 18 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def agg(self) -> FrameOrSeriesUnion | None:
kwargs = self.kwargs

if isinstance(arg, str):
return self.maybe_apply_str()
return self.apply_str()

if is_dict_like(arg):
return self.agg_dict_like()
Expand Down Expand Up @@ -456,7 +456,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion:

return result

def maybe_apply_str(self) -> FrameOrSeriesUnion:
def apply_str(self) -> FrameOrSeriesUnion:
"""
Compute apply in case of a string.

Expand All @@ -465,8 +465,7 @@ def maybe_apply_str(self) -> FrameOrSeriesUnion:
result: Series or DataFrame
"""
# Caller is responsible for checking isinstance(self.f, str)
f = self.f
f = cast(str, f)
f = cast(str, self.f)

obj = self.obj

Expand All @@ -482,7 +481,7 @@ def maybe_apply_str(self) -> FrameOrSeriesUnion:
raise ValueError(f"Operation {f} does not support axis=1")
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)

def maybe_apply_multiple(self) -> FrameOrSeriesUnion | None:
def apply_multiple(self) -> FrameOrSeriesUnion:
"""
Compute apply in case of a list-like or dict-like.

Expand All @@ -491,9 +490,6 @@ def maybe_apply_multiple(self) -> FrameOrSeriesUnion | None:
result: Series, DataFrame, or None
Result when self.f is a list-like or dict-like, None otherwise.
"""
# Note: dict-likes are list-like
if not is_list_like(self.f):
return None
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)

def normalize_dictlike_arg(
Expand Down Expand Up @@ -634,17 +630,16 @@ def dtypes(self) -> Series:
def apply(self) -> FrameOrSeriesUnion:
""" compute the results """
# dispatch to agg
result = self.maybe_apply_multiple()
if result is not None:
return result
if is_list_like(self.f):
return self.apply_multiple()

# all empty
if len(self.columns) == 0 and len(self.index) == 0:
return self.apply_empty_result()

# string dispatch
if isinstance(self.f, str):
return self.maybe_apply_str()
return self.apply_str()

# ufunc
elif isinstance(self.f, np.ufunc):
Expand Down Expand Up @@ -829,15 +824,15 @@ def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion

return result

def maybe_apply_str(self) -> FrameOrSeriesUnion:
def apply_str(self) -> FrameOrSeriesUnion:
# Caller is responsible for checking isinstance(self.f, str)
# TODO: GH#39993 - Avoid special-casing by replacing with lambda
if self.f == "size":
# Special-cased because DataFrame.size returns a single scalar
obj = self.obj
value = obj.shape[self.axis]
return obj._constructor_sliced(value, index=self.agg_axis, name="size")
return super().maybe_apply_str()
return super().apply_str()


class FrameRowApply(FrameApply):
Expand Down Expand Up @@ -1005,13 +1000,12 @@ def apply(self) -> FrameOrSeriesUnion:
return self.apply_empty_result()

# dispatch to agg
result = self.maybe_apply_multiple()
if result is not None:
return result
if is_list_like(self.f):
return self.apply_multiple()

if isinstance(self.f, str):
# if we are a string, try to dispatch
return self.maybe_apply_str()
return self.apply_str()

return self.apply_standard()

Expand Down