23
23
Type ,
24
24
TypeVar ,
25
25
Union ,
26
+ cast ,
26
27
)
27
28
import warnings
28
29
83
84
from pandas .plotting import boxplot_frame_groupby
84
85
85
86
if TYPE_CHECKING :
86
- from pandas .core .internals import Block
87
+ from pandas .core .internals import Block # noqa:F401
87
88
88
89
89
90
NamedAgg = namedtuple ("NamedAgg" , ["column" , "aggfunc" ])
@@ -1591,7 +1592,7 @@ def _gotitem(self, key, ndim: int, subset=None):
1591
1592
Parameters
1592
1593
----------
1593
1594
key : string / list of selections
1594
- ndim : 1,2
1595
+ ndim : {1, 2}
1595
1596
requested ndim of result
1596
1597
subset : object, default None
1597
1598
subset to act on
@@ -1617,7 +1618,7 @@ def _gotitem(self, key, ndim: int, subset=None):
1617
1618
1618
1619
raise AssertionError ("invalid ndim for _gotitem" )
1619
1620
1620
- def _wrap_frame_output (self , result , obj ) -> DataFrame :
1621
+ def _wrap_frame_output (self , result , obj : DataFrame ) -> DataFrame :
1621
1622
result_index = self .grouper .levels [0 ]
1622
1623
1623
1624
if self .axis == 0 :
@@ -1634,20 +1635,14 @@ def _get_data_to_aggregate(self) -> BlockManager:
1634
1635
else :
1635
1636
return obj ._mgr
1636
1637
1637
- def _insert_inaxis_grouper_inplace (self , result ) :
1638
+ def _insert_inaxis_grouper_inplace (self , result : DataFrame ) -> None :
1638
1639
# zip in reverse so we can always insert at loc 0
1639
- izip = zip (
1640
- * map (
1641
- reversed ,
1642
- (
1643
- self .grouper .names ,
1644
- self .grouper .get_group_levels (),
1645
- [grp .in_axis for grp in self .grouper .groupings ],
1646
- ),
1647
- )
1648
- )
1649
1640
columns = result .columns
1650
- for name , lev , in_axis in izip :
1641
+ for name , lev , in_axis in zip (
1642
+ reversed (self .grouper .names ),
1643
+ reversed (self .grouper .get_group_levels ()),
1644
+ reversed ([grp .in_axis for grp in self .grouper .groupings ]),
1645
+ ):
1651
1646
# GH #28549
1652
1647
# When using .apply(-), name will be in columns already
1653
1648
if in_axis and name not in columns :
@@ -1712,7 +1707,7 @@ def _wrap_transformed_output(
1712
1707
1713
1708
return result
1714
1709
1715
- def _wrap_agged_blocks (self , blocks : " Sequence[Block]" , items : Index ) -> DataFrame :
1710
+ def _wrap_agged_blocks (self , blocks : Sequence [" Block" ] , items : Index ) -> DataFrame :
1716
1711
if not self .as_index :
1717
1712
index = np .arange (blocks [0 ].values .shape [- 1 ])
1718
1713
mgr = BlockManager (blocks , axes = [items , index ])
@@ -1739,7 +1734,7 @@ def _iterate_column_groupbys(self):
1739
1734
exclusions = self .exclusions ,
1740
1735
)
1741
1736
1742
- def _apply_to_column_groupbys (self , func ):
1737
+ def _apply_to_column_groupbys (self , func ) -> DataFrame :
1743
1738
from pandas .core .reshape .concat import concat
1744
1739
1745
1740
return concat (
@@ -1748,7 +1743,7 @@ def _apply_to_column_groupbys(self, func):
1748
1743
axis = 1 ,
1749
1744
)
1750
1745
1751
- def count (self ):
1746
+ def count (self ) -> DataFrame :
1752
1747
"""
1753
1748
Compute count of group, excluding missing values.
1754
1749
@@ -1778,7 +1773,7 @@ def count(self):
1778
1773
1779
1774
return self ._reindex_output (result , fill_value = 0 )
1780
1775
1781
- def nunique (self , dropna : bool = True ):
1776
+ def nunique (self , dropna : bool = True ) -> DataFrame :
1782
1777
"""
1783
1778
Return DataFrame with counts of unique elements in each position.
1784
1779
@@ -1844,6 +1839,7 @@ def nunique(self, dropna: bool = True):
1844
1839
],
1845
1840
axis = 1 ,
1846
1841
)
1842
+ results = cast (DataFrame , results )
1847
1843
1848
1844
if axis_number == 1 :
1849
1845
results = results .T
0 commit comments