Skip to content

REF: Make _slice_take_blocks_ax0 a generator #58805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2024
Merged
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
68 changes: 31 additions & 37 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,13 @@ def reindex_indexer(
raise IndexError("Requested axis not found in manager")

if axis == 0:
new_blocks = self._slice_take_blocks_ax0(
indexer,
fill_value=fill_value,
only_slice=only_slice,
use_na_proxy=use_na_proxy,
new_blocks = list(
self._slice_take_blocks_ax0(
indexer,
fill_value=fill_value,
only_slice=only_slice,
use_na_proxy=use_na_proxy,
)
)
else:
new_blocks = [
Expand Down Expand Up @@ -857,7 +859,7 @@ def _slice_take_blocks_ax0(
*,
use_na_proxy: bool = False,
ref_inplace_op: bool = False,
) -> list[Block]:
) -> Generator[Block, None, None]:
"""
Slice/take blocks along axis=0.

Expand All @@ -875,9 +877,9 @@ def _slice_take_blocks_ax0(
ref_inplace_op: bool, default False
Don't track refs if True because we operate inplace

Returns
-------
new_blocks : list of Block
Yields
------
Block : New Block
"""
allow_fill = fill_value is not lib.no_default

Expand All @@ -892,35 +894,32 @@ def _slice_take_blocks_ax0(
# GH#32959 EABlock would fail since we can't make 0-width
# TODO(EA2D): special casing unnecessary with 2D EAs
if sllen == 0:
return []
return
bp = BlockPlacement(slice(0, sllen))
return [blk.getitem_block_columns(slobj, new_mgr_locs=bp)]
yield blk.getitem_block_columns(slobj, new_mgr_locs=bp)
return
elif not allow_fill or self.ndim == 1:
if allow_fill and fill_value is None:
fill_value = blk.fill_value

if not allow_fill and only_slice:
# GH#33597 slice instead of take, so we get
# views instead of copies
blocks = [
blk.getitem_block_columns(
for i, ml in enumerate(slobj):
yield blk.getitem_block_columns(
slice(ml, ml + 1),
new_mgr_locs=BlockPlacement(i),
ref_inplace_op=ref_inplace_op,
)
for i, ml in enumerate(slobj)
]
return blocks
else:
bp = BlockPlacement(slice(0, sllen))
return [
blk.take_nd(
slobj,
axis=0,
new_mgr_locs=bp,
fill_value=fill_value,
)
]
yield blk.take_nd(
slobj,
axis=0,
new_mgr_locs=bp,
fill_value=fill_value,
)
return

if sl_type == "slice":
blknos = self.blknos[slobj]
Expand All @@ -935,18 +934,15 @@ def _slice_take_blocks_ax0(

# When filling blknos, make sure blknos is updated before appending to
# blocks list, that way new blkno is exactly len(blocks).
blocks = []
group = not only_slice
for blkno, mgr_locs in libinternals.get_blkno_placements(blknos, group=group):
if blkno == -1:
# If we've got here, fill_value was not lib.no_default

blocks.append(
self._make_na_block(
placement=mgr_locs,
fill_value=fill_value,
use_na_proxy=use_na_proxy,
)
yield self._make_na_block(
placement=mgr_locs,
fill_value=fill_value,
use_na_proxy=use_na_proxy,
)
else:
blk = self.blocks[blkno]
Expand All @@ -961,7 +957,7 @@ def _slice_take_blocks_ax0(
for mgr_loc in mgr_locs:
newblk = blk.copy(deep=deep)
newblk.mgr_locs = BlockPlacement(slice(mgr_loc, mgr_loc + 1))
blocks.append(newblk)
yield newblk

else:
# GH#32779 to avoid the performance penalty of copying,
Expand All @@ -972,7 +968,7 @@ def _slice_take_blocks_ax0(

if isinstance(taker, slice):
nb = blk.getitem_block_columns(taker, new_mgr_locs=mgr_locs)
blocks.append(nb)
yield nb
elif only_slice:
# GH#33597 slice instead of take, so we get
# views instead of copies
Expand All @@ -981,12 +977,10 @@ def _slice_take_blocks_ax0(
bp = BlockPlacement(ml)
nb = blk.getitem_block_columns(slc, new_mgr_locs=bp)
# We have np.shares_memory(nb.values, blk.values)
blocks.append(nb)
yield nb
else:
nb = blk.take_nd(taker, axis=0, new_mgr_locs=mgr_locs)
blocks.append(nb)

return blocks
yield nb

def _make_na_block(
self, placement: BlockPlacement, fill_value=None, use_na_proxy: bool = False
Expand Down