@@ -503,6 +503,40 @@ def test_add_suffix(using_copy_on_write):
503
503
tm .assert_frame_equal (df , df_orig )
504
504
505
505
506
+ @pytest .mark .parametrize ("axis, val" , [(0 , 5.5 ), (1 , np .nan )])
507
+ def test_dropna (using_copy_on_write , axis , val ):
508
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , val , 6 ], "c" : "d" })
509
+ df_orig = df .copy ()
510
+ df2 = df .dropna (axis = axis )
511
+
512
+ if using_copy_on_write :
513
+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
514
+ else :
515
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
516
+
517
+ df2 .iloc [0 , 0 ] = 0
518
+ if using_copy_on_write :
519
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
520
+ tm .assert_frame_equal (df , df_orig )
521
+
522
+
523
+ @pytest .mark .parametrize ("val" , [5 , 5.5 ])
524
+ def test_dropna_series (using_copy_on_write , val ):
525
+ ser = Series ([1 , val , 4 ])
526
+ ser_orig = ser .copy ()
527
+ ser2 = ser .dropna ()
528
+
529
+ if using_copy_on_write :
530
+ assert np .shares_memory (ser2 .values , ser .values )
531
+ else :
532
+ assert not np .shares_memory (ser2 .values , ser .values )
533
+
534
+ ser2 .iloc [0 ] = 0
535
+ if using_copy_on_write :
536
+ assert not np .shares_memory (ser2 .values , ser .values )
537
+ tm .assert_series_equal (ser , ser_orig )
538
+
539
+
506
540
@pytest .mark .parametrize (
507
541
"method" ,
508
542
[
@@ -644,6 +678,45 @@ def test_sort_index(using_copy_on_write):
644
678
tm .assert_series_equal (ser , ser_orig )
645
679
646
680
681
+ @pytest .mark .parametrize (
682
+ "obj, kwargs" ,
683
+ [(Series ([1 , 2 , 3 ], name = "a" ), {}), (DataFrame ({"a" : [1 , 2 , 3 ]}), {"by" : "a" })],
684
+ )
685
+ def test_sort_values (using_copy_on_write , obj , kwargs ):
686
+ obj_orig = obj .copy ()
687
+ obj2 = obj .sort_values (** kwargs )
688
+
689
+ if using_copy_on_write :
690
+ assert np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
691
+ else :
692
+ assert not np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
693
+
694
+ # mutating df triggers a copy-on-write for the column / block
695
+ obj2 .iloc [0 ] = 0
696
+ assert not np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
697
+ tm .assert_equal (obj , obj_orig )
698
+
699
+
700
+ @pytest .mark .parametrize (
701
+ "obj, kwargs" ,
702
+ [(Series ([1 , 2 , 3 ], name = "a" ), {}), (DataFrame ({"a" : [1 , 2 , 3 ]}), {"by" : "a" })],
703
+ )
704
+ def test_sort_values_inplace (using_copy_on_write , obj , kwargs , using_array_manager ):
705
+ obj_orig = obj .copy ()
706
+ view = obj [:]
707
+ obj .sort_values (inplace = True , ** kwargs )
708
+
709
+ assert np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
710
+
711
+ # mutating obj triggers a copy-on-write for the column / block
712
+ obj .iloc [0 ] = 0
713
+ if using_copy_on_write :
714
+ assert not np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
715
+ tm .assert_equal (view , obj_orig )
716
+ else :
717
+ assert np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
718
+
719
+
647
720
def test_reorder_levels (using_copy_on_write ):
648
721
index = MultiIndex .from_tuples (
649
722
[(1 , 1 ), (1 , 2 ), (2 , 1 ), (2 , 2 )], names = ["one" , "two" ]
0 commit comments