@@ -1069,16 +1069,17 @@ def cast_agg_result(result, values: ArrayLike, how: str) -> ArrayLike:
10691069 # reshape to be valid for non-Extension Block
10701070 result = result .reshape (1 , - 1 )
10711071
1072+ elif isinstance (result , np .ndarray ) and result .ndim == 1 :
1073+ # We went through a SeriesGroupByPath and need to reshape
1074+ result = result .reshape (1 , - 1 )
1075+
10721076 return result
10731077
1074- def blk_func (block : "Block" ) -> List ["Block" ]:
1075- new_blocks : List ["Block" ] = []
1078+ def blk_func (bvalues : ArrayLike ) -> ArrayLike :
10761079
1077- result = no_result
1078- locs = block .mgr_locs .as_array
10791080 try :
10801081 result , _ = self .grouper .aggregate (
1081- block . values , how , axis = 1 , min_count = min_count
1082+ bvalues , how , axis = 1 , min_count = min_count
10821083 )
10831084 except NotImplementedError :
10841085 # generally if we have numeric_only=False
@@ -1091,12 +1092,17 @@ def blk_func(block: "Block") -> List["Block"]:
10911092 assert how == "ohlc"
10921093 raise
10931094
1095+ obj : Union [Series , DataFrame ]
10941096 # call our grouper again with only this block
1095- obj = self .obj [data .items [locs ]]
1096- if obj .shape [1 ] == 1 :
1097- # Avoid call to self.values that can occur in DataFrame
1098- # reductions; see GH#28949
1099- obj = obj .iloc [:, 0 ]
1097+ if isinstance (bvalues , ExtensionArray ):
1098+ # TODO(EA2D): special case not needed with 2D EAs
1099+ obj = Series (bvalues )
1100+ else :
1101+ obj = DataFrame (bvalues .T )
1102+ if obj .shape [1 ] == 1 :
1103+ # Avoid call to self.values that can occur in DataFrame
1104+ # reductions; see GH#28949
1105+ obj = obj .iloc [:, 0 ]
11001106
11011107 # Create SeriesGroupBy with observed=True so that it does
11021108 # not try to add missing categories if grouping over multiple
@@ -1114,21 +1120,14 @@ def blk_func(block: "Block") -> List["Block"]:
11141120
11151121 # unwrap DataFrame to get array
11161122 result = result ._mgr .blocks [0 ].values
1117- if isinstance (result , np .ndarray ) and result .ndim == 1 :
1118- result = result .reshape (1 , - 1 )
1119- res_values = cast_agg_result (result , block .values , how )
1120- agg_block = block .make_block (res_values )
1121- new_blocks = [agg_block ]
1122- else :
1123- res_values = cast_agg_result (result , block .values , how )
1124- agg_block = block .make_block (res_values )
1125- new_blocks = [agg_block ]
1126- return new_blocks
1123+
1124+ res_values = cast_agg_result (result , bvalues , how )
1125+ return res_values
11271126
11281127 skipped : List [int ] = []
11291128 for i , block in enumerate (data .blocks ):
11301129 try :
1131- nbs = blk_func ( block )
1130+ nbs = block . apply ( blk_func )
11321131 except (NotImplementedError , TypeError ):
11331132 # TypeError -> we may have an exception in trying to aggregate
11341133 # continue and exclude the block
0 commit comments