@@ -154,9 +154,6 @@ def _get_data_to_aggregate(
154154 )
155155 return single
156156
157- def _iterate_slices (self ) -> Iterable [Series ]:
158- yield self ._selected_obj
159-
160157 _agg_examples_doc = dedent (
161158 """
162159 Examples
@@ -408,7 +405,9 @@ def _aggregate_named(self, func, *args, **kwargs):
408405 result = {}
409406 initialized = False
410407
411- for name , group in self :
408+ for name , group in self .grouper .get_iterator (
409+ self ._selected_obj , axis = self .axis
410+ ):
412411 object .__setattr__ (group , "name" , name )
413412
414413 output = func (group , * args , ** kwargs )
@@ -568,7 +567,11 @@ def true_and_notna(x) -> bool:
568567
569568 try :
570569 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 )
572575 ]
573576 except (ValueError , TypeError ) as err :
574577 raise TypeError ("the filter must return a boolean result" ) from err
@@ -1850,29 +1853,33 @@ def _indexed_output_to_ndframe(
18501853 def _wrap_agged_manager (self , mgr : Manager2D ) -> DataFrame :
18511854 return self .obj ._constructor (mgr )
18521855
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 (
18561863 obj .iloc [:, i ],
18571864 selection = colname ,
18581865 grouper = self .grouper ,
18591866 exclusions = self .exclusions ,
18601867 observed = self .observed ,
18611868 )
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 )
18691870 ]
1871+ results = [func (sgb ) for sgb in sgbs ]
18701872
18711873 if not len (results ):
18721874 # concat would raise
1873- return DataFrame ([], columns = columns , index = self .grouper .result_index )
1875+ res_df = DataFrame ([], columns = columns , index = self .grouper .result_index )
18741876 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
18761883
18771884 def nunique (self , dropna : bool = True ) -> DataFrame :
18781885 """
@@ -1925,16 +1932,7 @@ def nunique(self, dropna: bool = True) -> DataFrame:
19251932 lambda sgb : sgb .nunique (dropna ), self ._obj_with_exclusions , is_agg = True
19261933 )
19271934
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 ))
19381936
19391937 def idxmax (
19401938 self ,
0 commit comments