Skip to content

Commit cba6b66

Browse files
rhshadrachyeshsurya
authored andcommitted
REF: maybe_apply_* in core.apply (pandas-dev#41224)
1 parent 4c4d3e3 commit cba6b66

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

pandas/core/apply.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def agg(self) -> FrameOrSeriesUnion | None:
156156
kwargs = self.kwargs
157157

158158
if isinstance(arg, str):
159-
return self.maybe_apply_str()
159+
return self.apply_str()
160160

161161
if is_dict_like(arg):
162162
return self.agg_dict_like()
@@ -456,7 +456,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion:
456456

457457
return result
458458

459-
def maybe_apply_str(self) -> FrameOrSeriesUnion:
459+
def apply_str(self) -> FrameOrSeriesUnion:
460460
"""
461461
Compute apply in case of a string.
462462
@@ -465,8 +465,7 @@ def maybe_apply_str(self) -> FrameOrSeriesUnion:
465465
result: Series or DataFrame
466466
"""
467467
# Caller is responsible for checking isinstance(self.f, str)
468-
f = self.f
469-
f = cast(str, f)
468+
f = cast(str, self.f)
470469

471470
obj = self.obj
472471

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

485-
def maybe_apply_multiple(self) -> FrameOrSeriesUnion | None:
484+
def apply_multiple(self) -> FrameOrSeriesUnion:
486485
"""
487486
Compute apply in case of a list-like or dict-like.
488487
@@ -491,9 +490,6 @@ def maybe_apply_multiple(self) -> FrameOrSeriesUnion | None:
491490
result: Series, DataFrame, or None
492491
Result when self.f is a list-like or dict-like, None otherwise.
493492
"""
494-
# Note: dict-likes are list-like
495-
if not is_list_like(self.f):
496-
return None
497493
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)
498494

499495
def normalize_dictlike_arg(
@@ -634,17 +630,16 @@ def dtypes(self) -> Series:
634630
def apply(self) -> FrameOrSeriesUnion:
635631
""" compute the results """
636632
# dispatch to agg
637-
result = self.maybe_apply_multiple()
638-
if result is not None:
639-
return result
633+
if is_list_like(self.f):
634+
return self.apply_multiple()
640635

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

645640
# string dispatch
646641
if isinstance(self.f, str):
647-
return self.maybe_apply_str()
642+
return self.apply_str()
648643

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

830825
return result
831826

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

842837

843838
class FrameRowApply(FrameApply):
@@ -1005,13 +1000,12 @@ def apply(self) -> FrameOrSeriesUnion:
10051000
return self.apply_empty_result()
10061001

10071002
# dispatch to agg
1008-
result = self.maybe_apply_multiple()
1009-
if result is not None:
1010-
return result
1003+
if is_list_like(self.f):
1004+
return self.apply_multiple()
10111005

10121006
if isinstance(self.f, str):
10131007
# if we are a string, try to dispatch
1014-
return self.maybe_apply_str()
1008+
return self.apply_str()
10151009

10161010
return self.apply_standard()
10171011

0 commit comments

Comments
 (0)