Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12195,6 +12195,7 @@ def _get_data() -> DataFrame:
# simple case where we can use BlockManager.reduce
res = df._mgr.reduce(blk_func)
out = df._constructor_from_mgr(res, axes=res.axes).iloc[0]
out.name = None
if out_dtype is not None and out.dtype != "boolean":
out = out.astype(out_dtype)
elif (df._mgr.get_dtypes() == object).any() and name not in ["any", "all"]:
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def apply(self, func, **kwargs) -> list[Block]:
return self._split_op_result(result)

@final
def reduce(self, func) -> list[Block]:
def reduce(self, func) -> Block:
# We will apply the function and reshape the result into a single-row
# Block with the same mgr_locs; squeezing will be done at a higher level
assert self.ndim == 2
Expand All @@ -362,8 +362,7 @@ def reduce(self, func) -> list[Block]:
else:
res_values = result.reshape(-1, 1)

nb = self.make_block(res_values)
return [nb]
return self.make_block(res_values)

@final
def _split_op_result(self, result: ArrayLike) -> list[Block]:
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,12 +1651,8 @@ def reduce(self, func: Callable) -> Self:
# If 2D, we assume that we're operating column-wise
assert self.ndim == 2

res_blocks: list[Block] = []
for blk in self.blocks:
nbs = blk.reduce(func)
res_blocks.extend(nbs)

index = Index([None]) # placeholder
res_blocks = [blk.reduce(func) for blk in self.blocks]
index = default_index(1) # placeholder
new_mgr = type(self).from_blocks(res_blocks, [self.items, index])
return new_mgr

Expand Down
Loading