Skip to content

Commit d969dd8

Browse files
authored
REF: Make _slice_take_blocks_ax0 a generator (pandas-dev#58805)
* REF: Make _slice_take_blocks_ax0 a generator * Remove []
1 parent 0d47e86 commit d969dd8

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

pandas/core/internals/managers.py

+31-37
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,13 @@ def reindex_indexer(
821821
raise IndexError("Requested axis not found in manager")
822822

823823
if axis == 0:
824-
new_blocks = self._slice_take_blocks_ax0(
825-
indexer,
826-
fill_value=fill_value,
827-
only_slice=only_slice,
828-
use_na_proxy=use_na_proxy,
824+
new_blocks = list(
825+
self._slice_take_blocks_ax0(
826+
indexer,
827+
fill_value=fill_value,
828+
only_slice=only_slice,
829+
use_na_proxy=use_na_proxy,
830+
)
829831
)
830832
else:
831833
new_blocks = [
@@ -857,7 +859,7 @@ def _slice_take_blocks_ax0(
857859
*,
858860
use_na_proxy: bool = False,
859861
ref_inplace_op: bool = False,
860-
) -> list[Block]:
862+
) -> Generator[Block, None, None]:
861863
"""
862864
Slice/take blocks along axis=0.
863865
@@ -875,9 +877,9 @@ def _slice_take_blocks_ax0(
875877
ref_inplace_op: bool, default False
876878
Don't track refs if True because we operate inplace
877879
878-
Returns
879-
-------
880-
new_blocks : list of Block
880+
Yields
881+
------
882+
Block : New Block
881883
"""
882884
allow_fill = fill_value is not lib.no_default
883885

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

902905
if not allow_fill and only_slice:
903906
# GH#33597 slice instead of take, so we get
904907
# views instead of copies
905-
blocks = [
906-
blk.getitem_block_columns(
908+
for i, ml in enumerate(slobj):
909+
yield blk.getitem_block_columns(
907910
slice(ml, ml + 1),
908911
new_mgr_locs=BlockPlacement(i),
909912
ref_inplace_op=ref_inplace_op,
910913
)
911-
for i, ml in enumerate(slobj)
912-
]
913-
return blocks
914914
else:
915915
bp = BlockPlacement(slice(0, sllen))
916-
return [
917-
blk.take_nd(
918-
slobj,
919-
axis=0,
920-
new_mgr_locs=bp,
921-
fill_value=fill_value,
922-
)
923-
]
916+
yield blk.take_nd(
917+
slobj,
918+
axis=0,
919+
new_mgr_locs=bp,
920+
fill_value=fill_value,
921+
)
922+
return
924923

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

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

944-
blocks.append(
945-
self._make_na_block(
946-
placement=mgr_locs,
947-
fill_value=fill_value,
948-
use_na_proxy=use_na_proxy,
949-
)
942+
yield self._make_na_block(
943+
placement=mgr_locs,
944+
fill_value=fill_value,
945+
use_na_proxy=use_na_proxy,
950946
)
951947
else:
952948
blk = self.blocks[blkno]
@@ -961,7 +957,7 @@ def _slice_take_blocks_ax0(
961957
for mgr_loc in mgr_locs:
962958
newblk = blk.copy(deep=deep)
963959
newblk.mgr_locs = BlockPlacement(slice(mgr_loc, mgr_loc + 1))
964-
blocks.append(newblk)
960+
yield newblk
965961

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

973969
if isinstance(taker, slice):
974970
nb = blk.getitem_block_columns(taker, new_mgr_locs=mgr_locs)
975-
blocks.append(nb)
971+
yield nb
976972
elif only_slice:
977973
# GH#33597 slice instead of take, so we get
978974
# views instead of copies
@@ -981,12 +977,10 @@ def _slice_take_blocks_ax0(
981977
bp = BlockPlacement(ml)
982978
nb = blk.getitem_block_columns(slc, new_mgr_locs=bp)
983979
# We have np.shares_memory(nb.values, blk.values)
984-
blocks.append(nb)
980+
yield nb
985981
else:
986982
nb = blk.take_nd(taker, axis=0, new_mgr_locs=mgr_locs)
987-
blocks.append(nb)
988-
989-
return blocks
983+
yield nb
990984

991985
def _make_na_block(
992986
self, placement: BlockPlacement, fill_value=None, use_na_proxy: bool = False

0 commit comments

Comments
 (0)