Skip to content

Commit fbb490c

Browse files
authored
CLN/TYP: aggregation methods in core.base (#36677)
1 parent 4ab9288 commit fbb490c

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

pandas/core/base.py

+31-21
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import builtins
66
import textwrap
7-
from typing import Any, Callable, Dict, FrozenSet, Optional, Union
7+
from typing import Any, Callable, Dict, FrozenSet, List, Optional, Union, cast
88

99
import numpy as np
1010

1111
import pandas._libs.lib as lib
12+
from pandas._typing import AggFuncType, AggFuncTypeBase, Label
1213
from pandas.compat import PYPY
1314
from pandas.compat.numpy import function as nv
1415
from pandas.errors import AbstractMethodError
@@ -278,7 +279,7 @@ def _try_aggregate_string_function(self, arg: str, *args, **kwargs):
278279
f"'{arg}' is not a valid function for '{type(self).__name__}' object"
279280
)
280281

281-
def _aggregate(self, arg, *args, **kwargs):
282+
def _aggregate(self, arg: AggFuncType, *args, **kwargs):
282283
"""
283284
provide an implementation for the aggregators
284285
@@ -311,13 +312,13 @@ def _aggregate(self, arg, *args, **kwargs):
311312
if _axis != 0: # pragma: no cover
312313
raise ValueError("Can only pass dict with axis=0")
313314

314-
obj = self._selected_obj
315+
selected_obj = self._selected_obj
315316

316317
# if we have a dict of any non-scalars
317318
# eg. {'A' : ['mean']}, normalize all to
318319
# be list-likes
319320
if any(is_aggregator(x) for x in arg.values()):
320-
new_arg = {}
321+
new_arg: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]] = {}
321322
for k, v in arg.items():
322323
if not isinstance(v, (tuple, list, dict)):
323324
new_arg[k] = [v]
@@ -336,9 +337,12 @@ def _aggregate(self, arg, *args, **kwargs):
336337
# {'ra' : { 'A' : 'mean' }}
337338
if isinstance(v, dict):
338339
raise SpecificationError("nested renamer is not supported")
339-
elif isinstance(obj, ABCSeries):
340+
elif isinstance(selected_obj, ABCSeries):
340341
raise SpecificationError("nested renamer is not supported")
341-
elif isinstance(obj, ABCDataFrame) and k not in obj.columns:
342+
elif (
343+
isinstance(selected_obj, ABCDataFrame)
344+
and k not in selected_obj.columns
345+
):
342346
raise KeyError(f"Column '{k}' does not exist!")
343347

344348
arg = new_arg
@@ -347,10 +351,12 @@ def _aggregate(self, arg, *args, **kwargs):
347351
# deprecation of renaming keys
348352
# GH 15931
349353
keys = list(arg.keys())
350-
if isinstance(obj, ABCDataFrame) and len(
351-
obj.columns.intersection(keys)
354+
if isinstance(selected_obj, ABCDataFrame) and len(
355+
selected_obj.columns.intersection(keys)
352356
) != len(keys):
353-
cols = sorted(set(keys) - set(obj.columns.intersection(keys)))
357+
cols = sorted(
358+
set(keys) - set(selected_obj.columns.intersection(keys))
359+
)
354360
raise SpecificationError(f"Column(s) {cols} do not exist")
355361

356362
from pandas.core.reshape.concat import concat
@@ -370,7 +376,7 @@ def _agg_2dim(how):
370376
"""
371377
aggregate a 2-dim with how
372378
"""
373-
colg = self._gotitem(self._selection, ndim=2, subset=obj)
379+
colg = self._gotitem(self._selection, ndim=2, subset=selected_obj)
374380
return colg.aggregate(how)
375381

376382
def _agg(arg, func):
@@ -385,7 +391,6 @@ def _agg(arg, func):
385391

386392
# set the final keys
387393
keys = list(arg.keys())
388-
result = {}
389394

390395
if self._selection is not None:
391396

@@ -473,7 +478,11 @@ def is_any_frame() -> bool:
473478
# we have a dict of scalars
474479

475480
# GH 36212 use name only if self is a series
476-
name = self.name if (self.ndim == 1) else None
481+
if self.ndim == 1:
482+
self = cast("Series", self)
483+
name = self.name
484+
else:
485+
name = None
477486

478487
result = Series(result, name=name)
479488

@@ -484,9 +493,10 @@ def is_any_frame() -> bool:
484493
else:
485494
result = None
486495

487-
f = self._get_cython_func(arg)
488-
if f and not args and not kwargs:
489-
return getattr(self, f)(), None
496+
if callable(arg):
497+
f = self._get_cython_func(arg)
498+
if f and not args and not kwargs:
499+
return getattr(self, f)(), None
490500

491501
# caller can react
492502
return result, True
@@ -498,17 +508,17 @@ def _aggregate_multiple_funcs(self, arg, _axis):
498508
raise NotImplementedError("axis other than 0 is not supported")
499509

500510
if self._selected_obj.ndim == 1:
501-
obj = self._selected_obj
511+
selected_obj = self._selected_obj
502512
else:
503-
obj = self._obj_with_exclusions
513+
selected_obj = self._obj_with_exclusions
504514

505515
results = []
506516
keys = []
507517

508518
# degenerate case
509-
if obj.ndim == 1:
519+
if selected_obj.ndim == 1:
510520
for a in arg:
511-
colg = self._gotitem(obj.name, ndim=1, subset=obj)
521+
colg = self._gotitem(selected_obj.name, ndim=1, subset=selected_obj)
512522
try:
513523
new_res = colg.aggregate(a)
514524

@@ -523,8 +533,8 @@ def _aggregate_multiple_funcs(self, arg, _axis):
523533

524534
# multiples
525535
else:
526-
for index, col in enumerate(obj):
527-
colg = self._gotitem(col, ndim=1, subset=obj.iloc[:, index])
536+
for index, col in enumerate(selected_obj):
537+
colg = self._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index])
528538
try:
529539
new_res = colg.aggregate(arg)
530540
except (TypeError, DataError):

0 commit comments

Comments
 (0)