1111from functools import partial
1212from textwrap import dedent
1313from typing import (
14+ TYPE_CHECKING ,
1415 Any ,
1516 Callable ,
1617 Hashable ,
1718 Iterable ,
1819 Mapping ,
1920 NamedTuple ,
2021 Sequence ,
22+ Tuple ,
2123 TypeVar ,
2224 Union ,
2325 cast ,
8890
8991from pandas .plotting import boxplot_frame_groupby
9092
93+ if TYPE_CHECKING :
94+ from pandas ._libs import Interval
95+
9196# TODO(typing) the return value on this callable should be any *scalar*.
9297AggScalar = Union [str , Callable [..., Any ]]
9398# TODO: validate types on ScalarResult and move to _typing
@@ -270,9 +275,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
270275 func = maybe_mangle_lambdas (func )
271276 ret = self ._aggregate_multiple_funcs (func )
272277 if relabeling :
273- # error: Incompatible types in assignment (expression has type
274- # "Optional[List[str]]", variable has type "Index")
275- ret .columns = columns # type: ignore[assignment]
278+ ret .columns = Index (columns )
276279 return ret
277280
278281 else :
@@ -622,33 +625,27 @@ def value_counts(
622625 ids , val = ids [mask ], val [mask ]
623626
624627 if bins is None :
625- lab , lev = algorithms .factorize (val , sort = True )
628+ lab , lev = cast (
629+ Tuple [np .ndarray , Index ], algorithms .factorize (val , sort = True )
630+ )
626631 llab = lambda lab , inc : lab [inc ]
627632 else :
628633
629- # lab is a Categorical with categories an IntervalIndex
630- lab = cut (Series (val ), bins , include_lowest = True )
631- # error: "ndarray" has no attribute "cat"
632- lev = lab .cat .categories # type: ignore[attr-defined]
633- # error: No overload variant of "take" of "_ArrayOrScalarCommon" matches
634- # argument types "Any", "bool", "Union[Any, float]"
635- lab = lev .take ( # type: ignore[call-overload]
636- # error: "ndarray" has no attribute "cat"
637- lab .cat .codes , # type: ignore[attr-defined]
634+ # labels is a Series with categories an IntervalIndex
635+ labels : Series = cut (Series (val ), bins , include_lowest = True )
636+ lev = labels .cat .categories
637+ lab = lev .take (
638+ labels .cat .codes ,
638639 allow_fill = True ,
639- # error: Item "ndarray" of "Union[ndarray, Index]" has no attribute
640- # "_na_value"
641- fill_value = lev ._na_value , # type: ignore[union-attr]
640+ fill_value = lev ._na_value ,
642641 )
643642 llab = lambda lab , inc : lab [inc ]._multiindex .codes [- 1 ]
644643
645644 if is_interval_dtype (lab .dtype ):
646645 # TODO: should we do this inside II?
647646
648- # error: "ndarray" has no attribute "left"
649- # error: "ndarray" has no attribute "right"
650647 sorter = np .lexsort (
651- (lab .left , lab .right , ids ) # type: ignore[attr-defined]
648+ (cast ( Interval , lab ) .left , cast ( Interval , lab ) .right , ids )
652649 )
653650 else :
654651 sorter = np .lexsort ((lab , ids ))
@@ -675,11 +672,7 @@ def value_counts(
675672 # multi-index components
676673 codes = self .grouper .reconstructed_codes
677674 codes = [rep (level_codes ) for level_codes in codes ] + [llab (lab , inc )]
678- # error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
679- # expected "Index"
680- levels = [ping .group_index for ping in self .grouper .groupings ] + [
681- lev # type: ignore[list-item]
682- ]
675+ levels = [ping .group_index for ping in self .grouper .groupings ] + [lev ]
683676
684677 if dropna :
685678 mask = codes [- 1 ] != - 1
0 commit comments