Skip to content

Commit 09a83d7

Browse files
authored
CLN: Use RangeIndex placeholder for Manager.reduce (#62972)
1 parent 2a4f647 commit 09a83d7

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12195,6 +12195,7 @@ def _get_data() -> DataFrame:
1219512195
# simple case where we can use BlockManager.reduce
1219612196
res = df._mgr.reduce(blk_func)
1219712197
out = df._constructor_from_mgr(res, axes=res.axes).iloc[0]
12198+
out.name = None
1219812199
if out_dtype is not None and out.dtype != "boolean":
1219912200
out = out.astype(out_dtype)
1220012201
elif (df._mgr.get_dtypes() == object).any() and name not in ["any", "all"]:

pandas/core/internals/blocks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def apply(self, func, **kwargs) -> list[Block]:
350350
return self._split_op_result(result)
351351

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

365-
nb = self.make_block(res_values)
366-
return [nb]
365+
return self.make_block(res_values)
367366

368367
@final
369368
def _split_op_result(self, result: ArrayLike) -> list[Block]:

pandas/core/internals/managers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,12 +1651,8 @@ def reduce(self, func: Callable) -> Self:
16511651
# If 2D, we assume that we're operating column-wise
16521652
assert self.ndim == 2
16531653

1654-
res_blocks: list[Block] = []
1655-
for blk in self.blocks:
1656-
nbs = blk.reduce(func)
1657-
res_blocks.extend(nbs)
1658-
1659-
index = Index([None]) # placeholder
1654+
res_blocks = [blk.reduce(func) for blk in self.blocks]
1655+
index = default_index(1) # placeholder
16601656
new_mgr = type(self).from_blocks(res_blocks, [self.items, index])
16611657
return new_mgr
16621658

0 commit comments

Comments
 (0)