@@ -156,7 +156,7 @@ def agg(self) -> FrameOrSeriesUnion | None:
156
156
kwargs = self .kwargs
157
157
158
158
if isinstance (arg , str ):
159
- return self .maybe_apply_str ()
159
+ return self .apply_str ()
160
160
161
161
if is_dict_like (arg ):
162
162
return self .agg_dict_like ()
@@ -456,7 +456,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion:
456
456
457
457
return result
458
458
459
- def maybe_apply_str (self ) -> FrameOrSeriesUnion :
459
+ def apply_str (self ) -> FrameOrSeriesUnion :
460
460
"""
461
461
Compute apply in case of a string.
462
462
@@ -465,8 +465,7 @@ def maybe_apply_str(self) -> FrameOrSeriesUnion:
465
465
result: Series or DataFrame
466
466
"""
467
467
# Caller is responsible for checking isinstance(self.f, str)
468
- f = self .f
469
- f = cast (str , f )
468
+ f = cast (str , self .f )
470
469
471
470
obj = self .obj
472
471
@@ -482,7 +481,7 @@ def maybe_apply_str(self) -> FrameOrSeriesUnion:
482
481
raise ValueError (f"Operation { f } does not support axis=1" )
483
482
return self ._try_aggregate_string_function (obj , f , * self .args , ** self .kwargs )
484
483
485
- def maybe_apply_multiple (self ) -> FrameOrSeriesUnion | None :
484
+ def apply_multiple (self ) -> FrameOrSeriesUnion :
486
485
"""
487
486
Compute apply in case of a list-like or dict-like.
488
487
@@ -491,9 +490,6 @@ def maybe_apply_multiple(self) -> FrameOrSeriesUnion | None:
491
490
result: Series, DataFrame, or None
492
491
Result when self.f is a list-like or dict-like, None otherwise.
493
492
"""
494
- # Note: dict-likes are list-like
495
- if not is_list_like (self .f ):
496
- return None
497
493
return self .obj .aggregate (self .f , self .axis , * self .args , ** self .kwargs )
498
494
499
495
def normalize_dictlike_arg (
@@ -634,17 +630,16 @@ def dtypes(self) -> Series:
634
630
def apply (self ) -> FrameOrSeriesUnion :
635
631
""" compute the results """
636
632
# 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 ()
640
635
641
636
# all empty
642
637
if len (self .columns ) == 0 and len (self .index ) == 0 :
643
638
return self .apply_empty_result ()
644
639
645
640
# string dispatch
646
641
if isinstance (self .f , str ):
647
- return self .maybe_apply_str ()
642
+ return self .apply_str ()
648
643
649
644
# ufunc
650
645
elif isinstance (self .f , np .ufunc ):
@@ -829,15 +824,15 @@ def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion
829
824
830
825
return result
831
826
832
- def maybe_apply_str (self ) -> FrameOrSeriesUnion :
827
+ def apply_str (self ) -> FrameOrSeriesUnion :
833
828
# Caller is responsible for checking isinstance(self.f, str)
834
829
# TODO: GH#39993 - Avoid special-casing by replacing with lambda
835
830
if self .f == "size" :
836
831
# Special-cased because DataFrame.size returns a single scalar
837
832
obj = self .obj
838
833
value = obj .shape [self .axis ]
839
834
return obj ._constructor_sliced (value , index = self .agg_axis , name = "size" )
840
- return super ().maybe_apply_str ()
835
+ return super ().apply_str ()
841
836
842
837
843
838
class FrameRowApply (FrameApply ):
@@ -1005,13 +1000,12 @@ def apply(self) -> FrameOrSeriesUnion:
1005
1000
return self .apply_empty_result ()
1006
1001
1007
1002
# 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 ()
1011
1005
1012
1006
if isinstance (self .f , str ):
1013
1007
# if we are a string, try to dispatch
1014
- return self .maybe_apply_str ()
1008
+ return self .apply_str ()
1015
1009
1016
1010
return self .apply_standard ()
1017
1011
0 commit comments