@@ -356,3 +356,37 @@ def test_reorder_levels(using_copy_on_write):
356
356
if using_copy_on_write :
357
357
assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
358
358
tm .assert_frame_equal (df , df_orig )
359
+
360
+
361
+ def test_frame_set_axis (using_copy_on_write ):
362
+ # GH 49473
363
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
364
+ df_orig = df .copy ()
365
+ df2 = df .set_axis (["a" , "b" , "c" ], axis = "index" )
366
+
367
+ if using_copy_on_write :
368
+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
369
+ else :
370
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
371
+
372
+ # mutating df2 triggers a copy-on-write for that column / block
373
+ df2 .iloc [0 , 0 ] = 0
374
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
375
+ tm .assert_frame_equal (df , df_orig )
376
+
377
+
378
+ def test_series_set_axis (using_copy_on_write ):
379
+ # GH 49473
380
+ ser = Series ([1 , 2 , 3 ])
381
+ ser_orig = ser .copy ()
382
+ ser2 = ser .set_axis (["a" , "b" , "c" ], axis = "index" )
383
+
384
+ if using_copy_on_write :
385
+ assert np .shares_memory (ser , ser2 )
386
+ else :
387
+ assert not np .shares_memory (ser , ser2 )
388
+
389
+ # mutating ser triggers a copy-on-write for the column / block
390
+ ser2 .iloc [0 ] = 0
391
+ assert not np .shares_memory (ser2 , ser )
392
+ tm .assert_series_equal (ser , ser_orig )
0 commit comments