@@ -278,7 +278,7 @@ def transform_str_or_callable(self, func) -> DataFrame | Series:
278
278
kwargs = self .kwargs
279
279
280
280
if isinstance (func , str ):
281
- return self ._try_aggregate_string_function (obj , func , * args , ** kwargs )
281
+ return self ._apply_str (obj , func , * args , ** kwargs )
282
282
283
283
if not args and not kwargs :
284
284
f = com .get_cython_func (func )
@@ -543,7 +543,7 @@ def apply_str(self) -> DataFrame | Series:
543
543
self .kwargs ["axis" ] = self .axis
544
544
else :
545
545
self .kwargs ["axis" ] = self .axis
546
- return self ._try_aggregate_string_function (obj , f , * self .args , ** self .kwargs )
546
+ return self ._apply_str (obj , f , * self .args , ** self .kwargs )
547
547
548
548
def apply_multiple (self ) -> DataFrame | Series :
549
549
"""
@@ -601,34 +601,32 @@ def normalize_dictlike_arg(
601
601
func = new_func
602
602
return func
603
603
604
- def _try_aggregate_string_function (self , obj , arg : str , * args , ** kwargs ):
604
+ def _apply_str (self , obj , arg : str , * args , ** kwargs ):
605
605
"""
606
606
if arg is a string, then try to operate on it:
607
- - try to find a function (or attribute) on ourselves
607
+ - try to find a function (or attribute) on obj
608
608
- try to find a numpy function
609
609
- raise
610
610
"""
611
611
assert isinstance (arg , str )
612
612
613
- f = getattr (obj , arg , None )
614
- if f is not None :
613
+ if hasattr (obj , arg ):
614
+ f = getattr ( obj , arg )
615
615
if callable (f ):
616
616
return f (* args , ** kwargs )
617
617
618
- # people may try to aggregate on a non-callable attribute
618
+ # people may aggregate on a non-callable attribute
619
619
# but don't let them think they can pass args to it
620
620
assert len (args ) == 0
621
621
assert len ([kwarg for kwarg in kwargs if kwarg not in ["axis" ]]) == 0
622
622
return f
623
-
624
- f = getattr (np , arg , None )
625
- if f is not None and hasattr (obj , "__array__" ):
623
+ elif hasattr (np , arg ) and hasattr (obj , "__array__" ):
626
624
# in particular exclude Window
625
+ f = getattr (np , arg )
627
626
return f (obj , * args , ** kwargs )
628
-
629
- raise AttributeError (
630
- f"'{ arg } ' is not a valid function for '{ type (obj ).__name__ } ' object"
631
- )
627
+ else :
628
+ msg = f"'{ arg } ' is not a valid function for '{ type (obj ).__name__ } ' object"
629
+ raise AttributeError (msg )
632
630
633
631
634
632
class NDFrameApply (Apply ):
0 commit comments