@@ -221,6 +221,27 @@ def test_select_dtypes(using_copy_on_write):
221
221
tm .assert_frame_equal (df , df_orig )
222
222
223
223
224
+ @pytest .mark .parametrize (
225
+ "filter_kwargs" , [{"items" : ["a" ]}, {"like" : "a" }, {"regex" : "a" }]
226
+ )
227
+ def test_filter (using_copy_on_write , filter_kwargs ):
228
+ # Case: selecting columns using `filter()` returns a new dataframe
229
+ # + afterwards modifying the result
230
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
231
+ df_orig = df .copy ()
232
+ df2 = df .filter (** filter_kwargs )
233
+ if using_copy_on_write :
234
+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
235
+ else :
236
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
237
+
238
+ # mutating df2 triggers a copy-on-write for that column/block
239
+ if using_copy_on_write :
240
+ df2 .iloc [0 , 0 ] = 0
241
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
242
+ tm .assert_frame_equal (df , df_orig )
243
+
244
+
224
245
def test_pop (using_copy_on_write ):
225
246
df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
226
247
df_orig = df .copy ()
0 commit comments