@@ -333,8 +333,7 @@ def test_replace_list_multiple_elements_inplace(using_copy_on_write):
333
333
arr = get_array (df , "a" )
334
334
df .replace ([1 , 2 ], 4 , inplace = True )
335
335
if using_copy_on_write :
336
- # TODO(CoW): This should share memory
337
- assert not np .shares_memory (arr , get_array (df , "a" ))
336
+ assert np .shares_memory (arr , get_array (df , "a" ))
338
337
assert df ._mgr ._has_no_reference (0 )
339
338
else :
340
339
assert np .shares_memory (arr , get_array (df , "a" ))
@@ -396,3 +395,38 @@ def test_replace_chained_assignment(using_copy_on_write):
396
395
with tm .raises_chained_assignment_error ():
397
396
df [["a" ]].replace (1 , 100 , inplace = True )
398
397
tm .assert_frame_equal (df , df_orig )
398
+
399
+
400
+ def test_replace_listlike (using_copy_on_write ):
401
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [1 , 2 , 3 ]})
402
+ df_orig = df .copy ()
403
+
404
+ result = df .replace ([200 , 201 ], [11 , 11 ])
405
+ if using_copy_on_write :
406
+ assert np .shares_memory (get_array (result , "a" ), get_array (df , "a" ))
407
+ else :
408
+ assert not np .shares_memory (get_array (result , "a" ), get_array (df , "a" ))
409
+
410
+ result .iloc [0 , 0 ] = 100
411
+ tm .assert_frame_equal (df , df )
412
+
413
+ result = df .replace ([200 , 2 ], [10 , 10 ])
414
+ assert not np .shares_memory (get_array (df , "a" ), get_array (result , "a" ))
415
+ tm .assert_frame_equal (df , df_orig )
416
+
417
+
418
+ def test_replace_listlike_inplace (using_copy_on_write ):
419
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [1 , 2 , 3 ]})
420
+ arr = get_array (df , "a" )
421
+ df .replace ([200 , 2 ], [10 , 11 ], inplace = True )
422
+ assert np .shares_memory (get_array (df , "a" ), arr )
423
+
424
+ view = df [:]
425
+ df_orig = df .copy ()
426
+ df .replace ([200 , 3 ], [10 , 11 ], inplace = True )
427
+ if using_copy_on_write :
428
+ assert not np .shares_memory (get_array (df , "a" ), arr )
429
+ tm .assert_frame_equal (view , df_orig )
430
+ else :
431
+ assert np .shares_memory (get_array (df , "a" ), arr )
432
+ tm .assert_frame_equal (df , view )
0 commit comments