@@ -456,6 +456,41 @@ def test_align_series(using_copy_on_write):
456
456
tm .assert_series_equal (ser_other , ser_orig )
457
457
458
458
459
+ def test_align_copy_false (using_copy_on_write ):
460
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
461
+ df_orig = df .copy ()
462
+ df2 , df3 = df .align (df , copy = False )
463
+
464
+ assert np .shares_memory (get_array (df , "b" ), get_array (df2 , "b" ))
465
+ assert np .shares_memory (get_array (df , "a" ), get_array (df2 , "a" ))
466
+
467
+ if using_copy_on_write :
468
+ df2 .loc [0 , "a" ] = 0
469
+ tm .assert_frame_equal (df , df_orig ) # Original is unchanged
470
+
471
+ df3 .loc [0 , "a" ] = 0
472
+ tm .assert_frame_equal (df , df_orig ) # Original is unchanged
473
+
474
+
475
+ def test_align_with_series_copy_false (using_copy_on_write ):
476
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
477
+ ser = Series ([1 , 2 , 3 ], name = "x" )
478
+ ser_orig = ser .copy ()
479
+ df_orig = df .copy ()
480
+ df2 , ser2 = df .align (ser , copy = False , axis = 0 )
481
+
482
+ assert np .shares_memory (get_array (df , "b" ), get_array (df2 , "b" ))
483
+ assert np .shares_memory (get_array (df , "a" ), get_array (df2 , "a" ))
484
+ assert np .shares_memory (get_array (ser , "x" ), get_array (ser2 , "x" ))
485
+
486
+ if using_copy_on_write :
487
+ df2 .loc [0 , "a" ] = 0
488
+ tm .assert_frame_equal (df , df_orig ) # Original is unchanged
489
+
490
+ ser2 .loc [0 ] = 0
491
+ tm .assert_series_equal (ser , ser_orig ) # Original is unchanged
492
+
493
+
459
494
def test_to_frame (using_copy_on_write ):
460
495
# Case: converting a Series to a DataFrame with to_frame
461
496
ser = Series ([1 , 2 , 3 ])
0 commit comments