Skip to content

Commit 0e199f3

Browse files
authored
REF: simplify _cython_agg_blocks (pandas-dev#35841)
1 parent 712ffbd commit 0e199f3

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

pandas/core/groupby/generic.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Tuple,
2525
Type,
2626
Union,
27-
cast,
2827
)
2928
import warnings
3029

@@ -1100,24 +1099,19 @@ def blk_func(block: "Block") -> List["Block"]:
11001099
# continue and exclude the block
11011100
raise
11021101
else:
1103-
result = cast(DataFrame, result)
1102+
assert isinstance(result, (Series, DataFrame)) # for mypy
1103+
# In the case of object dtype block, it may have been split
1104+
# in the operation. We un-split here.
1105+
result = result._consolidate()
1106+
assert isinstance(result, (Series, DataFrame)) # for mypy
1107+
assert len(result._mgr.blocks) == 1
1108+
11041109
# unwrap DataFrame to get array
1105-
if len(result._mgr.blocks) != 1:
1106-
# We've split an object block! Everything we've assumed
1107-
# about a single block input returning a single block output
1108-
# is a lie. To keep the code-path for the typical non-split case
1109-
# clean, we choose to clean up this mess later on.
1110-
assert len(locs) == result.shape[1]
1111-
for i, loc in enumerate(locs):
1112-
agg_block = result.iloc[:, [i]]._mgr.blocks[0]
1113-
agg_block.mgr_locs = [loc]
1114-
new_blocks.append(agg_block)
1115-
else:
1116-
result = result._mgr.blocks[0].values
1117-
if isinstance(result, np.ndarray) and result.ndim == 1:
1118-
result = result.reshape(1, -1)
1119-
agg_block = cast_result_block(result, block, how)
1120-
new_blocks = [agg_block]
1110+
result = result._mgr.blocks[0].values
1111+
if isinstance(result, np.ndarray) and result.ndim == 1:
1112+
result = result.reshape(1, -1)
1113+
agg_block = cast_result_block(result, block, how)
1114+
new_blocks = [agg_block]
11211115
else:
11221116
agg_block = cast_result_block(result, block, how)
11231117
new_blocks = [agg_block]

0 commit comments

Comments
 (0)