diff --git a/pandas/_typing.py b/pandas/_typing.py index a9177106535fc..2244be1b61fc6 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -109,12 +109,14 @@ # types of `func` kwarg for DataFrame.aggregate and Series.aggregate AggFuncTypeBase = Union[Callable, str] +AggFuncTypeDict = Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]] AggFuncType = Union[ AggFuncTypeBase, List[AggFuncTypeBase], - Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], + AggFuncTypeDict, ] + # for arbitrary kwargs passed during reading/writing files StorageOptions = Optional[Dict[str, Any]] diff --git a/pandas/core/aggregation.py b/pandas/core/aggregation.py index 639b5f31835d1..ffc3283152687 100644 --- a/pandas/core/aggregation.py +++ b/pandas/core/aggregation.py @@ -23,6 +23,7 @@ from pandas._typing import ( AggFuncType, AggFuncTypeBase, + AggFuncTypeDict, Axis, FrameOrSeries, FrameOrSeriesUnion, @@ -442,7 +443,7 @@ def transform( func = {col: func for col in obj} if is_dict_like(func): - func = cast(Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], func) + func = cast(AggFuncTypeDict, func) return transform_dict_like(obj, func, *args, **kwargs) # func is either str or callable @@ -466,7 +467,7 @@ def transform( def transform_dict_like( obj: FrameOrSeries, - func: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], + func: AggFuncTypeDict, *args, **kwargs, ): @@ -560,7 +561,7 @@ def aggregate( if isinstance(arg, str): return obj._try_aggregate_string_function(arg, *args, **kwargs), None elif is_dict_like(arg): - arg = cast(Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], arg) + arg = cast(AggFuncTypeDict, arg) return agg_dict_like(obj, arg, _axis), True elif is_list_like(arg): # we require a list, but not an 'str' @@ -672,7 +673,7 @@ def agg_list_like( def agg_dict_like( obj, - arg: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]], + arg: AggFuncTypeDict, _axis: int, ) -> FrameOrSeriesUnion: """ @@ -701,7 +702,7 @@ def agg_dict_like( # eg. {'A' : ['mean']}, normalize all to # be list-likes if any(is_aggregator(x) for x in arg.values()): - new_arg: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]] = {} + new_arg: AggFuncTypeDict = {} for k, v in arg.items(): if not isinstance(v, (tuple, list, dict)): new_arg[k] = [v]