@@ -1069,16 +1069,17 @@ def cast_agg_result(result, values: ArrayLike, how: str) -> ArrayLike:
1069
1069
# reshape to be valid for non-Extension Block
1070
1070
result = result .reshape (1 , - 1 )
1071
1071
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
+
1072
1076
return result
1073
1077
1074
- def blk_func (block : "Block" ) -> List ["Block" ]:
1075
- new_blocks : List ["Block" ] = []
1078
+ def blk_func (bvalues : ArrayLike ) -> ArrayLike :
1076
1079
1077
- result = no_result
1078
- locs = block .mgr_locs .as_array
1079
1080
try :
1080
1081
result , _ = self .grouper .aggregate (
1081
- block . values , how , axis = 1 , min_count = min_count
1082
+ bvalues , how , axis = 1 , min_count = min_count
1082
1083
)
1083
1084
except NotImplementedError :
1084
1085
# generally if we have numeric_only=False
@@ -1091,12 +1092,17 @@ def blk_func(block: "Block") -> List["Block"]:
1091
1092
assert how == "ohlc"
1092
1093
raise
1093
1094
1095
+ obj : Union [Series , DataFrame ]
1094
1096
# 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 ]
1100
1106
1101
1107
# Create SeriesGroupBy with observed=True so that it does
1102
1108
# not try to add missing categories if grouping over multiple
@@ -1114,21 +1120,14 @@ def blk_func(block: "Block") -> List["Block"]:
1114
1120
1115
1121
# unwrap DataFrame to get array
1116
1122
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
1127
1126
1128
1127
skipped : List [int ] = []
1129
1128
for i , block in enumerate (data .blocks ):
1130
1129
try :
1131
- nbs = blk_func ( block )
1130
+ nbs = block . apply ( blk_func )
1132
1131
except (NotImplementedError , TypeError ):
1133
1132
# TypeError -> we may have an exception in trying to aggregate
1134
1133
# continue and exclude the block
0 commit comments