@@ -154,9 +154,6 @@ def _get_data_to_aggregate(
154
154
)
155
155
return single
156
156
157
- def _iterate_slices (self ) -> Iterable [Series ]:
158
- yield self ._selected_obj
159
-
160
157
_agg_examples_doc = dedent (
161
158
"""
162
159
Examples
@@ -408,7 +405,9 @@ def _aggregate_named(self, func, *args, **kwargs):
408
405
result = {}
409
406
initialized = False
410
407
411
- for name , group in self :
408
+ for name , group in self .grouper .get_iterator (
409
+ self ._selected_obj , axis = self .axis
410
+ ):
412
411
object .__setattr__ (group , "name" , name )
413
412
414
413
output = func (group , * args , ** kwargs )
@@ -568,7 +567,11 @@ def true_and_notna(x) -> bool:
568
567
569
568
try :
570
569
indices = [
571
- self ._get_index (name ) for name , group in self if true_and_notna (group )
570
+ self ._get_index (name )
571
+ for name , group in self .grouper .get_iterator (
572
+ self ._selected_obj , axis = self .axis
573
+ )
574
+ if true_and_notna (group )
572
575
]
573
576
except (ValueError , TypeError ) as err :
574
577
raise TypeError ("the filter must return a boolean result" ) from err
@@ -1850,29 +1853,33 @@ def _indexed_output_to_ndframe(
1850
1853
def _wrap_agged_manager (self , mgr : Manager2D ) -> DataFrame :
1851
1854
return self .obj ._constructor (mgr )
1852
1855
1853
- def _iterate_column_groupbys (self , obj : DataFrame ):
1854
- for i , colname in enumerate (obj .columns ):
1855
- yield colname , SeriesGroupBy (
1856
+ def _apply_to_column_groupbys (self , func ) -> DataFrame :
1857
+ from pandas .core .reshape .concat import concat
1858
+
1859
+ obj = self ._obj_with_exclusions
1860
+ columns = obj .columns
1861
+ sgbs = [
1862
+ SeriesGroupBy (
1856
1863
obj .iloc [:, i ],
1857
1864
selection = colname ,
1858
1865
grouper = self .grouper ,
1859
1866
exclusions = self .exclusions ,
1860
1867
observed = self .observed ,
1861
1868
)
1862
-
1863
- def _apply_to_column_groupbys (self , func , obj : DataFrame ) -> DataFrame :
1864
- from pandas .core .reshape .concat import concat
1865
-
1866
- columns = obj .columns
1867
- results = [
1868
- func (col_groupby ) for _ , col_groupby in self ._iterate_column_groupbys (obj )
1869
+ for i , colname in enumerate (obj .columns )
1869
1870
]
1871
+ results = [func (sgb ) for sgb in sgbs ]
1870
1872
1871
1873
if not len (results ):
1872
1874
# concat would raise
1873
- return DataFrame ([], columns = columns , index = self .grouper .result_index )
1875
+ res_df = DataFrame ([], columns = columns , index = self .grouper .result_index )
1874
1876
else :
1875
- return concat (results , keys = columns , axis = 1 )
1877
+ res_df = concat (results , keys = columns , axis = 1 )
1878
+
1879
+ if not self .as_index :
1880
+ res_df .index = default_index (len (res_df ))
1881
+ res_df = self ._insert_inaxis_grouper (res_df )
1882
+ return res_df
1876
1883
1877
1884
def nunique (self , dropna : bool = True ) -> DataFrame :
1878
1885
"""
@@ -1925,16 +1932,7 @@ def nunique(self, dropna: bool = True) -> DataFrame:
1925
1932
lambda sgb : sgb .nunique (dropna ), self ._obj_with_exclusions , is_agg = True
1926
1933
)
1927
1934
1928
- obj = self ._obj_with_exclusions
1929
- results = self ._apply_to_column_groupbys (
1930
- lambda sgb : sgb .nunique (dropna ), obj = obj
1931
- )
1932
-
1933
- if not self .as_index :
1934
- results .index = default_index (len (results ))
1935
- results = self ._insert_inaxis_grouper (results )
1936
-
1937
- return results
1935
+ return self ._apply_to_column_groupbys (lambda sgb : sgb .nunique (dropna ))
1938
1936
1939
1937
def idxmax (
1940
1938
self ,
0 commit comments