Skip to content

ENH: Add lazy copy to pipe #50567

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 4 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5771,7 +5771,10 @@ def pipe(
... .pipe((func, 'arg2'), arg1=a, arg3=c)
... ) # doctest: +SKIP
"""
return common.pipe(self, func, *args, **kwargs)
result = common.pipe(self, func, *args, **kwargs)
if _using_copy_on_write():
return result.copy(deep=None)
Copy link
Member

Choose a reason for hiding this comment

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

Should we pass in self.copy(deep=None) to the pipe, instead of shallow copying the result?

Or do we have guarantees at the moment about that a piped function can mutate the dataframe inplace?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, this should be the other way round, will adjust accordingly and add tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved and added another test modifying the object inplace

return result

# ----------------------------------------------------------------------
# Attribute access
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ def test_rename_columns_modify_parent(using_copy_on_write):
tm.assert_frame_equal(df2, df2_orig)


def test_pipe(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": 1.5})
df_orig = df.copy()

def testfunc(df):
return df

df2 = df.pipe(testfunc)

assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

# mutating df2 triggers a copy-on-write for that column
df2.iloc[0, 0] = 0
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
expected = DataFrame({"a": [0, 2, 3], "b": 1.5})
tm.assert_frame_equal(df, expected)

assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))


def test_reindex_columns(using_copy_on_write):
# Case: reindexing the column returns a new dataframe
# + afterwards modifying the result
Expand Down