Skip to content

CoW: Avoid tracking references in delete if not necessary #54111

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 8 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 3 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,16 @@ def take_block_columns(self, indices: npt.NDArray[np.intp]) -> Self:

@final
def getitem_block_columns(
self, slicer: slice, new_mgr_locs: BlockPlacement
self, slicer: slice, new_mgr_locs: BlockPlacement, ref_inplace_op: bool = False
) -> Self:
"""
Perform __getitem__-like, return result as block.

Only supports slices that preserve dimensionality.
"""
new_values = self._slice(slicer)
return type(self)(new_values, new_mgr_locs, self.ndim, refs=self.refs)
refs = self.refs if not ref_inplace_op or self.refs.has_reference() else None
return type(self)(new_values, new_mgr_locs, self.ndim, refs=refs)

@final
def _can_hold_element(self, element: Any) -> bool:
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ def _slice_take_blocks_ax0(
only_slice: bool = False,
*,
use_na_proxy: bool = False,
ref_inplace_op: bool = False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the docstring?

) -> list[Block]:
"""
Slice/take blocks along axis=0.
Expand Down Expand Up @@ -717,7 +718,9 @@ def _slice_take_blocks_ax0(
# views instead of copies
blocks = [
blk.getitem_block_columns(
slice(ml, ml + 1), new_mgr_locs=BlockPlacement(i)
slice(ml, ml + 1),
new_mgr_locs=BlockPlacement(i),
ref_inplace_op=ref_inplace_op,
)
for i, ml in enumerate(slobj)
]
Expand Down Expand Up @@ -1377,7 +1380,7 @@ def idelete(self, indexer) -> BlockManager:
is_deleted[indexer] = True
taker = (~is_deleted).nonzero()[0]

nbs = self._slice_take_blocks_ax0(taker, only_slice=True)
nbs = self._slice_take_blocks_ax0(taker, only_slice=True, ref_inplace_op=True)
new_columns = self.items[~is_deleted]
axes = [new_columns, self.axes[1]]
return type(self)(tuple(nbs), axes, verify_integrity=False)
Expand Down
16 changes: 12 additions & 4 deletions pandas/tests/copy_view/test_core_functionalities.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ def test_delete(using_copy_on_write):
df = DataFrame(np.random.randn(4, 3), columns=["a", "b", "c"])
del df["b"]
if using_copy_on_write:
# TODO: This should not have references, delete makes a shallow copy
# but keeps the blocks alive
assert df._mgr.blocks[0].refs.has_reference()
assert df._mgr.blocks[1].refs.has_reference()
assert not df._mgr.blocks[0].refs.has_reference()
assert not df._mgr.blocks[1].refs.has_reference()

df = df[["a"]]
if using_copy_on_write:
assert not df._mgr.blocks[0].refs.has_reference()


def test_delete_reference(using_copy_on_write):
df = DataFrame(np.random.randn(4, 3), columns=["a", "b", "c"])
x = df[:]
del df["b"]
if using_copy_on_write:
assert df._mgr.blocks[0].refs.has_reference()
assert df._mgr.blocks[1].refs.has_reference()
assert x._mgr.blocks[1].refs.has_reference()