Skip to content

Commit 18c4365

Browse files
authored
ENH: Add lazy copy to pipe (#50567)
1 parent 857bf37 commit 18c4365

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -5771,6 +5771,8 @@ def pipe(
57715771
... .pipe((func, 'arg2'), arg1=a, arg3=c)
57725772
... ) # doctest: +SKIP
57735773
"""
5774+
if using_copy_on_write():
5775+
return common.pipe(self.copy(deep=None), func, *args, **kwargs)
57745776
return common.pipe(self, func, *args, **kwargs)
57755777

57765778
# ----------------------------------------------------------------------

pandas/tests/copy_view/test_methods.py

+47
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,53 @@ def test_rename_columns_modify_parent(using_copy_on_write):
115115
tm.assert_frame_equal(df2, df2_orig)
116116

117117

118+
def test_pipe(using_copy_on_write):
119+
df = DataFrame({"a": [1, 2, 3], "b": 1.5})
120+
df_orig = df.copy()
121+
122+
def testfunc(df):
123+
return df
124+
125+
df2 = df.pipe(testfunc)
126+
127+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
128+
129+
# mutating df2 triggers a copy-on-write for that column
130+
df2.iloc[0, 0] = 0
131+
if using_copy_on_write:
132+
tm.assert_frame_equal(df, df_orig)
133+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
134+
else:
135+
expected = DataFrame({"a": [0, 2, 3], "b": 1.5})
136+
tm.assert_frame_equal(df, expected)
137+
138+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
139+
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
140+
141+
142+
def test_pipe_modify_df(using_copy_on_write):
143+
df = DataFrame({"a": [1, 2, 3], "b": 1.5})
144+
df_orig = df.copy()
145+
146+
def testfunc(df):
147+
df.iloc[0, 0] = 100
148+
return df
149+
150+
df2 = df.pipe(testfunc)
151+
152+
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
153+
154+
if using_copy_on_write:
155+
tm.assert_frame_equal(df, df_orig)
156+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
157+
else:
158+
expected = DataFrame({"a": [100, 2, 3], "b": 1.5})
159+
tm.assert_frame_equal(df, expected)
160+
161+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
162+
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
163+
164+
118165
def test_reindex_columns(using_copy_on_write):
119166
# Case: reindexing the column returns a new dataframe
120167
# + afterwards modifying the result

0 commit comments

Comments
 (0)