Skip to content

Commit 8145ea6

Browse files
authored
REF: _cython_agg_blocks follow patterns similar to _apply_blockwise (#35632)
1 parent bf8e9ef commit 8145ea6

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

pandas/core/groupby/generic.py

+26-15
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,7 @@ def _cython_agg_blocks(
10261026
if numeric_only:
10271027
data = data.get_numeric_data(copy=False)
10281028

1029-
agg_blocks: List[Block] = []
1030-
new_items: List[np.ndarray] = []
1029+
agg_blocks: List["Block"] = []
10311030
deleted_items: List[np.ndarray] = []
10321031

10331032
no_result = object()
@@ -1056,11 +1055,12 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
10561055
# reshape to be valid for non-Extension Block
10571056
result = result.reshape(1, -1)
10581057

1059-
agg_block: Block = block.make_block(result)
1058+
agg_block: "Block" = block.make_block(result)
10601059
return agg_block
10611060

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+
10641064
result = no_result
10651065
locs = block.mgr_locs.as_array
10661066
try:
@@ -1076,8 +1076,7 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
10761076
# we cannot perform the operation
10771077
# in an alternate way, exclude the block
10781078
assert how == "ohlc"
1079-
deleted_items.append(locs)
1080-
continue
1079+
raise
10811080

10821081
# call our grouper again with only this block
10831082
obj = self.obj[data.items[locs]]
@@ -1096,8 +1095,7 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
10961095
except TypeError:
10971096
# we may have an exception in trying to aggregate
10981097
# continue and exclude the block
1099-
deleted_items.append(locs)
1100-
continue
1098+
raise
11011099
else:
11021100
result = cast(DataFrame, result)
11031101
# unwrap DataFrame to get array
@@ -1108,20 +1106,33 @@ def cast_result_block(result, block: "Block", how: str) -> "Block":
11081106
# clean, we choose to clean up this mess later on.
11091107
assert len(locs) == result.shape[1]
11101108
for i, loc in enumerate(locs):
1111-
new_items.append(np.array([loc], dtype=locs.dtype))
11121109
agg_block = result.iloc[:, [i]]._mgr.blocks[0]
1113-
agg_blocks.append(agg_block)
1110+
new_blocks.append(agg_block)
11141111
else:
11151112
result = result._mgr.blocks[0].values
11161113
if isinstance(result, np.ndarray) and result.ndim == 1:
11171114
result = result.reshape(1, -1)
11181115
agg_block = cast_result_block(result, block, how)
1119-
new_items.append(locs)
1120-
agg_blocks.append(agg_block)
1116+
new_blocks = [agg_block]
11211117
else:
11221118
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)
11251136

11261137
if not agg_blocks:
11271138
raise DataError("No numeric types to aggregate")

0 commit comments

Comments
 (0)