32
32
AggObjType ,
33
33
Axis ,
34
34
NDFrameT ,
35
+ npt ,
35
36
)
36
37
from pandas .errors import (
37
38
DataError ,
@@ -590,18 +591,17 @@ def normalize_dictlike_arg(
590
591
cols_sorted = list (safe_sort (list (cols )))
591
592
raise KeyError (f"Column(s) { cols_sorted } do not exist" )
592
593
593
- is_aggregator = lambda x : isinstance ( x , ( list , tuple , dict ) )
594
+ aggregator_types = ( list , tuple , dict )
594
595
595
596
# if we have a dict of any non-scalars
596
597
# eg. {'A' : ['mean']}, normalize all to
597
598
# be list-likes
598
599
# Cannot use func.values() because arg may be a Series
599
- if any (is_aggregator ( x ) for _ , x in func .items ()):
600
+ if any (isinstance ( x , aggregator_types ) for _ , x in func .items ()):
600
601
new_func : AggFuncTypeDict = {}
601
602
for k , v in func .items ():
602
- if not is_aggregator (v ):
603
- # mypy can't realize v is not a list here
604
- new_func [k ] = [v ] # type: ignore[list-item]
603
+ if not isinstance (v , aggregator_types ):
604
+ new_func [k ] = [v ]
605
605
else :
606
606
new_func [k ] = v
607
607
func = new_func
@@ -1085,6 +1085,7 @@ def apply(self) -> DataFrame | Series:
1085
1085
# if we are a string, try to dispatch
1086
1086
return self .apply_str ()
1087
1087
1088
+ # self.f is Callable
1088
1089
return self .apply_standard ()
1089
1090
1090
1091
def agg (self ):
@@ -1122,7 +1123,8 @@ def apply_empty_result(self) -> Series:
1122
1123
)
1123
1124
1124
1125
def apply_standard (self ) -> DataFrame | Series :
1125
- f = self .f
1126
+ # caller is responsible for ensuring that f is Callable
1127
+ f = cast (Callable , self .f )
1126
1128
obj = self .obj
1127
1129
1128
1130
with np .errstate (all = "ignore" ):
@@ -1135,14 +1137,9 @@ def apply_standard(self) -> DataFrame | Series:
1135
1137
mapped = obj ._values .map (f )
1136
1138
else :
1137
1139
values = obj .astype (object )._values
1138
- # error: Argument 2 to "map_infer" has incompatible type
1139
- # "Union[Callable[..., Any], str, List[Union[Callable[..., Any], str]],
1140
- # Dict[Hashable, Union[Union[Callable[..., Any], str],
1141
- # List[Union[Callable[..., Any], str]]]]]"; expected
1142
- # "Callable[[Any], Any]"
1143
1140
mapped = lib .map_infer (
1144
1141
values ,
1145
- f , # type: ignore[arg-type]
1142
+ f ,
1146
1143
convert = self .convert_dtype ,
1147
1144
)
1148
1145
@@ -1211,7 +1208,7 @@ def transform(self):
1211
1208
1212
1209
def reconstruct_func (
1213
1210
func : AggFuncType | None , ** kwargs
1214
- ) -> tuple [bool , AggFuncType | None , list [str ] | None , list [ int ] | None ]:
1211
+ ) -> tuple [bool , AggFuncType | None , list [str ] | None , npt . NDArray [ np . intp ] | None ]:
1215
1212
"""
1216
1213
This is the internal function to reconstruct func given if there is relabeling
1217
1214
or not and also normalize the keyword to get new order of columns.
@@ -1238,7 +1235,7 @@ def reconstruct_func(
1238
1235
relabelling: bool, if there is relabelling or not
1239
1236
func: normalized and mangled func
1240
1237
columns: list of column names
1241
- order: list of columns indices
1238
+ order: array of columns indices
1242
1239
1243
1240
Examples
1244
1241
--------
@@ -1250,7 +1247,7 @@ def reconstruct_func(
1250
1247
"""
1251
1248
relabeling = func is None and is_multi_agg_with_relabel (** kwargs )
1252
1249
columns : list [str ] | None = None
1253
- order : list [ int ] | None = None
1250
+ order : npt . NDArray [ np . intp ] | None = None
1254
1251
1255
1252
if not relabeling :
1256
1253
if isinstance (func , list ) and len (func ) > len (set (func )):
@@ -1297,7 +1294,9 @@ def is_multi_agg_with_relabel(**kwargs) -> bool:
1297
1294
)
1298
1295
1299
1296
1300
- def normalize_keyword_aggregation (kwargs : dict ) -> tuple [dict , list [str ], list [int ]]:
1297
+ def normalize_keyword_aggregation (
1298
+ kwargs : dict ,
1299
+ ) -> tuple [dict , list [str ], npt .NDArray [np .intp ]]:
1301
1300
"""
1302
1301
Normalize user-provided "named aggregation" kwargs.
1303
1302
Transforms from the new ``Mapping[str, NamedAgg]`` style kwargs
@@ -1351,9 +1350,7 @@ def normalize_keyword_aggregation(kwargs: dict) -> tuple[dict, list[str], list[i
1351
1350
1352
1351
# get the new index of columns by comparison
1353
1352
col_idx_order = Index (uniquified_aggspec ).get_indexer (uniquified_order )
1354
- # error: Incompatible return value type (got "Tuple[defaultdict[Any, Any],
1355
- # Any, ndarray]", expected "Tuple[Dict[Any, Any], List[str], List[int]]")
1356
- return aggspec , columns , col_idx_order # type: ignore[return-value]
1353
+ return aggspec , columns , col_idx_order
1357
1354
1358
1355
1359
1356
def _make_unique_kwarg_list (
0 commit comments