Skip to content

CoW: Avoid unnecessary copies for columnwise replace #54117

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
Jul 17, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4158,11 +4158,15 @@ def _set_item_mgr(
if len(self):
self._check_setitem_copy()

def _iset_item(self, loc: int, value: Series) -> None:
def _iset_item(self, loc: int, value: Series, inplace: bool = True) -> None:
# We are only called from _replace_columnwise which guarantees that
# no reindex is necessary
# TODO(CoW): Optimize to avoid copy here, but have ton track refs
self._iset_item_mgr(loc, value._values.copy(), inplace=True)
if using_copy_on_write():
self._iset_item_mgr(
loc, value._values, inplace=inplace, refs=value._references
)
else:
self._iset_item_mgr(loc, value._values.copy(), inplace=True)

# check if we are modifying a copy
# try to set first as we want an invalid
Expand Down Expand Up @@ -5480,7 +5484,7 @@ def _replace_columnwise(
target, value = mapping[ax_value]
newobj = ser.replace(target, value, regex=regex)

res._iset_item(i, newobj)
res._iset_item(i, newobj, inplace=inplace)

if inplace:
return
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,9 @@ def value_getitem(placement):
if inplace and blk.should_store(value):
# Updating inplace -> check if we need to do Copy-on-Write
if using_copy_on_write() and not self._has_no_reference_block(blkno_l):
self._iset_split_block(blkno_l, blk_locs, value_getitem(val_locs))
self._iset_split_block(
blkno_l, blk_locs, value_getitem(val_locs), refs=refs
)
else:
blk.set_inplace(blk_locs, value_getitem(val_locs))
continue
Expand Down
14 changes: 12 additions & 2 deletions pandas/tests/copy_view/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,22 @@ def test_replace_list_none_inplace_refs(using_copy_on_write):
assert np.shares_memory(arr, get_array(df, "a"))


def test_replace_columnwise_no_op_inplace(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
view = df[:]
df_orig = df.copy()
df.replace({"a": 10}, 100, inplace=True)
if using_copy_on_write:
assert np.shares_memory(get_array(view, "a"), get_array(df, "a"))
df.iloc[0, 0] = 100
tm.assert_frame_equal(view, df_orig)


def test_replace_columnwise_no_op(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
df_orig = df.copy()
df2 = df.replace({"a": 10}, 100)
if using_copy_on_write:
# TODO(CoW): This should share memory
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
df2.iloc[0, 0] = 100
tm.assert_frame_equal(df, df_orig)