@@ -115,6 +115,53 @@ def test_rename_columns_modify_parent(using_copy_on_write):
115
115
tm .assert_frame_equal (df2 , df2_orig )
116
116
117
117
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
+
118
165
def test_reindex_columns (using_copy_on_write ):
119
166
# Case: reindexing the column returns a new dataframe
120
167
# + afterwards modifying the result
0 commit comments