From 9f7621764ac27ca58bbb3d534b8afea6807a5e26 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sun, 2 Nov 2025 15:06:12 -0800 Subject: [PATCH 1/2] Use RangeIndex placeholder for DataFrame.reduce --- pandas/core/frame.py | 1 + pandas/core/internals/managers.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5f62b5c07a5cf..1d80337ece551 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -12180,6 +12180,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"]: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 40ba74fed49d1..5ec608a4dc94c 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1656,7 +1656,7 @@ def reduce(self, func: Callable) -> Self: nbs = blk.reduce(func) res_blocks.extend(nbs) - index = Index([None]) # placeholder + index = default_index(1) # placeholder new_mgr = type(self).from_blocks(res_blocks, [self.items, index]) return new_mgr From ef2ebf3be3a7b3aee2ab9b8263c9f1fd99a1c3a4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sun, 2 Nov 2025 17:57:08 -0800 Subject: [PATCH 2/2] Make reduce return single block --- pandas/core/internals/blocks.py | 5 ++--- pandas/core/internals/managers.py | 6 +----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a9ad561cbc393..a602b4a575bf5 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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 @@ -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]: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 5ec608a4dc94c..186a2ddc44a56 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1651,11 +1651,7 @@ 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) - + 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