Skip to content

Commit 4c9fa96

Browse files
authored
CLN/TYP: Alias for aggregation dictionary argument (#37531)
1 parent 499b5ef commit 4c9fa96

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pandas/_typing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@
109109

110110
# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
111111
AggFuncTypeBase = Union[Callable, str]
112+
AggFuncTypeDict = Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]]
112113
AggFuncType = Union[
113114
AggFuncTypeBase,
114115
List[AggFuncTypeBase],
115-
Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]],
116+
AggFuncTypeDict,
116117
]
117118

119+
118120
# for arbitrary kwargs passed during reading/writing files
119121
StorageOptions = Optional[Dict[str, Any]]
120122

pandas/core/aggregation.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pandas._typing import (
2424
AggFuncType,
2525
AggFuncTypeBase,
26+
AggFuncTypeDict,
2627
Axis,
2728
FrameOrSeries,
2829
FrameOrSeriesUnion,
@@ -442,7 +443,7 @@ def transform(
442443
func = {col: func for col in obj}
443444

444445
if is_dict_like(func):
445-
func = cast(Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], func)
446+
func = cast(AggFuncTypeDict, func)
446447
return transform_dict_like(obj, func, *args, **kwargs)
447448

448449
# func is either str or callable
@@ -466,7 +467,7 @@ def transform(
466467

467468
def transform_dict_like(
468469
obj: FrameOrSeries,
469-
func: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]],
470+
func: AggFuncTypeDict,
470471
*args,
471472
**kwargs,
472473
):
@@ -560,7 +561,7 @@ def aggregate(
560561
if isinstance(arg, str):
561562
return obj._try_aggregate_string_function(arg, *args, **kwargs), None
562563
elif is_dict_like(arg):
563-
arg = cast(Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], arg)
564+
arg = cast(AggFuncTypeDict, arg)
564565
return agg_dict_like(obj, arg, _axis), True
565566
elif is_list_like(arg):
566567
# we require a list, but not an 'str'
@@ -672,7 +673,7 @@ def agg_list_like(
672673

673674
def agg_dict_like(
674675
obj,
675-
arg: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]],
676+
arg: AggFuncTypeDict,
676677
_axis: int,
677678
) -> FrameOrSeriesUnion:
678679
"""
@@ -701,7 +702,7 @@ def agg_dict_like(
701702
# eg. {'A' : ['mean']}, normalize all to
702703
# be list-likes
703704
if any(is_aggregator(x) for x in arg.values()):
704-
new_arg: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]] = {}
705+
new_arg: AggFuncTypeDict = {}
705706
for k, v in arg.items():
706707
if not isinstance(v, (tuple, list, dict)):
707708
new_arg[k] = [v]

0 commit comments

Comments
 (0)