4141 ArrayLike ,
4242 FrameOrSeries ,
4343 FrameOrSeriesUnion ,
44+ Manager ,
4445)
4546from pandas .util ._decorators import (
4647 Appender ,
107108 all_indexes_same ,
108109)
109110import pandas .core .indexes .base as ibase
110- from pandas .core .internals import BlockManager
111+ from pandas .core .internals import (
112+ ArrayManager ,
113+ BlockManager ,
114+ )
111115from pandas .core .series import Series
112116from pandas .core .util .numba_ import maybe_use_numba
113117
@@ -1074,20 +1078,22 @@ def _iterate_slices(self) -> Iterable[Series]:
10741078 def _cython_agg_general (
10751079 self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
10761080 ) -> DataFrame :
1077- agg_mgr = self ._cython_agg_blocks (
1081+ agg_mgr = self ._cython_agg_manager (
10781082 how , alt = alt , numeric_only = numeric_only , min_count = min_count
10791083 )
10801084 return self ._wrap_agged_manager (agg_mgr )
10811085
1082- def _cython_agg_blocks (
1086+ def _cython_agg_manager (
10831087 self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
1084- ) -> BlockManager :
1088+ ) -> Manager :
10851089
1086- data : BlockManager = self ._get_data_to_aggregate ()
1090+ data : Manager = self ._get_data_to_aggregate ()
10871091
10881092 if numeric_only :
10891093 data = data .get_numeric_data (copy = False )
10901094
1095+ using_array_manager = isinstance (data , ArrayManager )
1096+
10911097 def cast_agg_result (result , values : ArrayLike , how : str ) -> ArrayLike :
10921098 # see if we can cast the values to the desired dtype
10931099 # this may not be the original dtype
@@ -1101,7 +1107,11 @@ def cast_agg_result(result, values: ArrayLike, how: str) -> ArrayLike:
11011107 result = type (values )._from_sequence (result .ravel (), dtype = values .dtype )
11021108 # Note this will have result.dtype == dtype from above
11031109
1104- elif isinstance (result , np .ndarray ) and result .ndim == 1 :
1110+ elif (
1111+ not using_array_manager
1112+ and isinstance (result , np .ndarray )
1113+ and result .ndim == 1
1114+ ):
11051115 # We went through a SeriesGroupByPath and need to reshape
11061116 # GH#32223 includes case with IntegerArray values
11071117 result = result .reshape (1 , - 1 )
@@ -1153,11 +1163,11 @@ def py_fallback(bvalues: ArrayLike) -> ArrayLike:
11531163 result = mgr .blocks [0 ].values
11541164 return result
11551165
1156- def blk_func ( bvalues : ArrayLike ) -> ArrayLike :
1166+ def array_func ( values : ArrayLike ) -> ArrayLike :
11571167
11581168 try :
11591169 result = self .grouper ._cython_operation (
1160- "aggregate" , bvalues , how , axis = 1 , min_count = min_count
1170+ "aggregate" , values , how , axis = 1 , min_count = min_count
11611171 )
11621172 except NotImplementedError :
11631173 # generally if we have numeric_only=False
@@ -1170,14 +1180,14 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike:
11701180 assert how == "ohlc"
11711181 raise
11721182
1173- result = py_fallback (bvalues )
1183+ result = py_fallback (values )
11741184
1175- return cast_agg_result (result , bvalues , how )
1185+ return cast_agg_result (result , values , how )
11761186
11771187 # TypeError -> we may have an exception in trying to aggregate
11781188 # continue and exclude the block
11791189 # NotImplementedError -> "ohlc" with wrong dtype
1180- new_mgr = data .grouped_reduce (blk_func , ignore_failures = True )
1190+ new_mgr = data .grouped_reduce (array_func , ignore_failures = True )
11811191
11821192 if not len (new_mgr ):
11831193 raise DataError ("No numeric types to aggregate" )
@@ -1670,7 +1680,7 @@ def _wrap_frame_output(self, result, obj: DataFrame) -> DataFrame:
16701680 else :
16711681 return self .obj ._constructor (result , index = obj .index , columns = result_index )
16721682
1673- def _get_data_to_aggregate (self ) -> BlockManager :
1683+ def _get_data_to_aggregate (self ) -> Manager :
16741684 obj = self ._obj_with_exclusions
16751685 if self .axis == 1 :
16761686 return obj .T ._mgr
@@ -1755,17 +1765,17 @@ def _wrap_transformed_output(
17551765
17561766 return result
17571767
1758- def _wrap_agged_manager (self , mgr : BlockManager ) -> DataFrame :
1768+ def _wrap_agged_manager (self , mgr : Manager ) -> DataFrame :
17591769 if not self .as_index :
17601770 index = np .arange (mgr .shape [1 ])
1761- mgr .axes [ 1 ] = ibase .Index (index )
1771+ mgr .set_axis ( 1 , ibase .Index (index ), verify_integrity = False )
17621772 result = self .obj ._constructor (mgr )
17631773
17641774 self ._insert_inaxis_grouper_inplace (result )
17651775 result = result ._consolidate ()
17661776 else :
17671777 index = self .grouper .result_index
1768- mgr .axes [ 1 ] = index
1778+ mgr .set_axis ( 1 , index , verify_integrity = False )
17691779 result = self .obj ._constructor (mgr )
17701780
17711781 if self .axis == 1 :
0 commit comments