@@ -644,6 +644,45 @@ def test_sort_index(using_copy_on_write):
644644 tm .assert_series_equal (ser , ser_orig )
645645
646646
647+ @pytest .mark .parametrize (
648+ "obj, kwargs" ,
649+ [(Series ([1 , 2 , 3 ], name = "a" ), {}), (DataFrame ({"a" : [1 , 2 , 3 ]}), {"by" : "a" })],
650+ )
651+ def test_sort_values (using_copy_on_write , obj , kwargs ):
652+ obj_orig = obj .copy ()
653+ obj2 = obj .sort_values (** kwargs )
654+
655+ if using_copy_on_write :
656+ assert np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
657+ else :
658+ assert not np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
659+
660+ # mutating df triggers a copy-on-write for the column / block
661+ obj2 .iloc [0 ] = 0
662+ assert not np .shares_memory (get_array (obj2 , "a" ), get_array (obj , "a" ))
663+ tm .assert_equal (obj , obj_orig )
664+
665+
666+ @pytest .mark .parametrize (
667+ "obj, kwargs" ,
668+ [(Series ([1 , 2 , 3 ], name = "a" ), {}), (DataFrame ({"a" : [1 , 2 , 3 ]}), {"by" : "a" })],
669+ )
670+ def test_sort_values_inplace (using_copy_on_write , obj , kwargs , using_array_manager ):
671+ obj_orig = obj .copy ()
672+ view = obj [:]
673+ obj .sort_values (inplace = True , ** kwargs )
674+
675+ assert np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
676+
677+ # mutating obj triggers a copy-on-write for the column / block
678+ obj .iloc [0 ] = 0
679+ if using_copy_on_write :
680+ assert not np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
681+ tm .assert_equal (view , obj_orig )
682+ else :
683+ assert np .shares_memory (get_array (obj , "a" ), get_array (view , "a" ))
684+
685+
647686def test_reorder_levels (using_copy_on_write ):
648687 index = MultiIndex .from_tuples (
649688 [(1 , 1 ), (1 , 2 ), (2 , 1 ), (2 , 2 )], names = ["one" , "two" ]
0 commit comments