@@ -1026,8 +1026,7 @@ def _cython_agg_blocks(
1026
1026
if numeric_only :
1027
1027
data = data .get_numeric_data (copy = False )
1028
1028
1029
- agg_blocks : List [Block ] = []
1030
- new_items : List [np .ndarray ] = []
1029
+ agg_blocks : List ["Block" ] = []
1031
1030
deleted_items : List [np .ndarray ] = []
1032
1031
1033
1032
no_result = object ()
@@ -1056,11 +1055,12 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
1056
1055
# reshape to be valid for non-Extension Block
1057
1056
result = result .reshape (1 , - 1 )
1058
1057
1059
- agg_block : Block = block .make_block (result )
1058
+ agg_block : " Block" = block .make_block (result )
1060
1059
return agg_block
1061
1060
1062
- for block in data .blocks :
1063
- # Avoid inheriting result from earlier in the loop
1061
+ def blk_func (block : "Block" ) -> List ["Block" ]:
1062
+ new_blocks : List ["Block" ] = []
1063
+
1064
1064
result = no_result
1065
1065
locs = block .mgr_locs .as_array
1066
1066
try :
@@ -1076,8 +1076,7 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
1076
1076
# we cannot perform the operation
1077
1077
# in an alternate way, exclude the block
1078
1078
assert how == "ohlc"
1079
- deleted_items .append (locs )
1080
- continue
1079
+ raise
1081
1080
1082
1081
# call our grouper again with only this block
1083
1082
obj = self .obj [data .items [locs ]]
@@ -1096,8 +1095,7 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
1096
1095
except TypeError :
1097
1096
# we may have an exception in trying to aggregate
1098
1097
# continue and exclude the block
1099
- deleted_items .append (locs )
1100
- continue
1098
+ raise
1101
1099
else :
1102
1100
result = cast (DataFrame , result )
1103
1101
# unwrap DataFrame to get array
@@ -1108,20 +1106,33 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
1108
1106
# clean, we choose to clean up this mess later on.
1109
1107
assert len (locs ) == result .shape [1 ]
1110
1108
for i , loc in enumerate (locs ):
1111
- new_items .append (np .array ([loc ], dtype = locs .dtype ))
1112
1109
agg_block = result .iloc [:, [i ]]._mgr .blocks [0 ]
1113
- agg_blocks .append (agg_block )
1110
+ new_blocks .append (agg_block )
1114
1111
else :
1115
1112
result = result ._mgr .blocks [0 ].values
1116
1113
if isinstance (result , np .ndarray ) and result .ndim == 1 :
1117
1114
result = result .reshape (1 , - 1 )
1118
1115
agg_block = cast_result_block (result , block , how )
1119
- new_items .append (locs )
1120
- agg_blocks .append (agg_block )
1116
+ new_blocks = [agg_block ]
1121
1117
else :
1122
1118
agg_block = cast_result_block (result , block , how )
1123
- new_items .append (locs )
1124
- agg_blocks .append (agg_block )
1119
+ new_blocks = [agg_block ]
1120
+ return new_blocks
1121
+
1122
+ skipped : List [int ] = []
1123
+ new_items : List [np .ndarray ] = []
1124
+ for i , block in enumerate (data .blocks ):
1125
+ try :
1126
+ nbs = blk_func (block )
1127
+ except (NotImplementedError , TypeError ):
1128
+ # TypeError -> we may have an exception in trying to aggregate
1129
+ # continue and exclude the block
1130
+ # NotImplementedError -> "ohlc" with wrong dtype
1131
+ skipped .append (i )
1132
+ deleted_items .append (block .mgr_locs .as_array )
1133
+ else :
1134
+ agg_blocks .extend (nbs )
1135
+ new_items .append (block .mgr_locs .as_array )
1125
1136
1126
1137
if not agg_blocks :
1127
1138
raise DataError ("No numeric types to aggregate" )
0 commit comments