@@ -367,6 +367,36 @@ def test_reindex_columns(using_copy_on_write):
367367 tm .assert_frame_equal (df , df_orig )
368368
369369
370+ @pytest .mark .parametrize (
371+ "index" ,
372+ [
373+ lambda idx : idx ,
374+ lambda idx : idx .view (),
375+ lambda idx : idx .copy (),
376+ lambda idx : list (idx ),
377+ ],
378+ ids = ["identical" , "view" , "copy" , "values" ],
379+ )
380+ def test_reindex_rows (index , using_copy_on_write ):
381+ # Case: reindexing the rows with an index that matches the current index
382+ # can use a shallow copy
383+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
384+ df_orig = df .copy ()
385+ df2 = df .reindex (index = index (df .index ))
386+
387+ if using_copy_on_write :
388+ # still shares memory (df2 is a shallow copy)
389+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
390+ else :
391+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
392+ # mutating df2 triggers a copy-on-write for that column
393+ df2 .iloc [0 , 0 ] = 0
394+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
395+ if using_copy_on_write :
396+ assert np .shares_memory (get_array (df2 , "c" ), get_array (df , "c" ))
397+ tm .assert_frame_equal (df , df_orig )
398+
399+
370400def test_drop_on_column (using_copy_on_write ):
371401 df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
372402 df_orig = df .copy ()
0 commit comments