Skip to content

CLN/TYP: Alias for aggregation dictionary argument #37531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
11 changes: 6 additions & 5 deletions pandas/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pandas._typing import (
AggFuncType,
AggFuncTypeBase,
AggFuncTypeDict,
Axis,
FrameOrSeries,
FrameOrSeriesUnion,
Expand Down Expand Up @@ -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
Expand All @@ -466,7 +467,7 @@ def transform(

def transform_dict_like(
obj: FrameOrSeries,
func: Dict[Label, Union[AggFuncTypeBase, List[AggFuncTypeBase]]],
func: AggFuncTypeDict,
*args,
**kwargs,
):
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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]
Expand Down