11
11
from functools import partial
12
12
from textwrap import dedent
13
13
from typing import (
14
+ TYPE_CHECKING ,
14
15
Any ,
15
16
Callable ,
16
17
Hashable ,
17
18
Iterable ,
18
19
Mapping ,
19
20
NamedTuple ,
20
21
Sequence ,
22
+ Tuple ,
21
23
TypeVar ,
22
24
Union ,
23
25
cast ,
88
90
89
91
from pandas .plotting import boxplot_frame_groupby
90
92
93
+ if TYPE_CHECKING :
94
+ from pandas ._libs import Interval
95
+
91
96
# TODO(typing) the return value on this callable should be any *scalar*.
92
97
AggScalar = Union [str , Callable [..., Any ]]
93
98
# 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)
270
275
func = maybe_mangle_lambdas (func )
271
276
ret = self ._aggregate_multiple_funcs (func )
272
277
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 )
276
279
return ret
277
280
278
281
else :
@@ -622,33 +625,27 @@ def value_counts(
622
625
ids , val = ids [mask ], val [mask ]
623
626
624
627
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
+ )
626
631
llab = lambda lab , inc : lab [inc ]
627
632
else :
628
633
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 ,
638
639
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 ,
642
641
)
643
642
llab = lambda lab , inc : lab [inc ]._multiindex .codes [- 1 ]
644
643
645
644
if is_interval_dtype (lab .dtype ):
646
645
# TODO: should we do this inside II?
647
646
648
- # error: "ndarray" has no attribute "left"
649
- # error: "ndarray" has no attribute "right"
650
647
sorter = np .lexsort (
651
- (lab .left , lab .right , ids ) # type: ignore[attr-defined]
648
+ (cast ( Interval , lab ) .left , cast ( Interval , lab ) .right , ids )
652
649
)
653
650
else :
654
651
sorter = np .lexsort ((lab , ids ))
@@ -675,11 +672,7 @@ def value_counts(
675
672
# multi-index components
676
673
codes = self .grouper .reconstructed_codes
677
674
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 ]
683
676
684
677
if dropna :
685
678
mask = codes [- 1 ] != - 1
0 commit comments