Skip to content

Commit eb4658f

Browse files
CLN: clean Apply._try_aggregate_string_function (#53079)
* CLN: Apply._try_aggregate_string_function * rename _try_aggregate_string_function -> _apply_string_function --------- Co-authored-by: Richard Shadrach <[email protected]>
1 parent 030cf5d commit eb4658f

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

pandas/core/apply.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def transform_str_or_callable(self, func) -> DataFrame | Series:
278278
kwargs = self.kwargs
279279

280280
if isinstance(func, str):
281-
return self._try_aggregate_string_function(obj, func, *args, **kwargs)
281+
return self._apply_str(obj, func, *args, **kwargs)
282282

283283
if not args and not kwargs:
284284
f = com.get_cython_func(func)
@@ -543,7 +543,7 @@ def apply_str(self) -> DataFrame | Series:
543543
self.kwargs["axis"] = self.axis
544544
else:
545545
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)
547547

548548
def apply_multiple(self) -> DataFrame | Series:
549549
"""
@@ -601,34 +601,32 @@ def normalize_dictlike_arg(
601601
func = new_func
602602
return func
603603

604-
def _try_aggregate_string_function(self, obj, arg: str, *args, **kwargs):
604+
def _apply_str(self, obj, arg: str, *args, **kwargs):
605605
"""
606606
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
608608
- try to find a numpy function
609609
- raise
610610
"""
611611
assert isinstance(arg, str)
612612

613-
f = getattr(obj, arg, None)
614-
if f is not None:
613+
if hasattr(obj, arg):
614+
f = getattr(obj, arg)
615615
if callable(f):
616616
return f(*args, **kwargs)
617617

618-
# people may try to aggregate on a non-callable attribute
618+
# people may aggregate on a non-callable attribute
619619
# but don't let them think they can pass args to it
620620
assert len(args) == 0
621621
assert len([kwarg for kwarg in kwargs if kwarg not in ["axis"]]) == 0
622622
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__"):
626624
# in particular exclude Window
625+
f = getattr(np, arg)
627626
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)
632630

633631

634632
class NDFrameApply(Apply):

0 commit comments

Comments
 (0)