@@ -112,7 +112,8 @@ def test_replace_to_replace_wrong_dtype(using_copy_on_write):
112112 assert not np .shares_memory (get_array (df , "b" ), get_array (df2 , "b" ))
113113
114114
115- def test_replace_inplace (using_copy_on_write ):
115+ @pytest .mark .parametrize ("to_replace" , [1.5 , [1.5 ], []])
116+ def test_replace_inplace (using_copy_on_write , to_replace ):
116117 df = DataFrame ({"a" : [1.5 , 2 , 3 ]})
117118 arr_a = get_array (df , "a" )
118119 df .replace (to_replace = 1.5 , value = 15.5 , inplace = True )
@@ -216,3 +217,71 @@ def test_masking_inplace(using_copy_on_write, method):
216217 tm .assert_frame_equal (view , df_orig )
217218 else :
218219 assert np .shares_memory (get_array (df , "a" ), arr_a )
220+
221+
222+ def test_replace_empty_list (using_copy_on_write ):
223+ df = DataFrame ({"a" : [1 , 2 ]})
224+
225+ df2 = df .replace ([], [])
226+ if using_copy_on_write :
227+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
228+ assert not df ._mgr ._has_no_reference (0 )
229+ else :
230+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
231+
232+ arr_a = get_array (df , "a" )
233+ df .replace ([], [])
234+ if using_copy_on_write :
235+ assert np .shares_memory (get_array (df , "a" ), arr_a )
236+ assert not df ._mgr ._has_no_reference (0 )
237+ assert not df2 ._mgr ._has_no_reference (0 )
238+
239+
240+ @pytest .mark .parametrize ("value" , ["d" , None ])
241+ def test_replace_object_list_inplace (using_copy_on_write , value ):
242+ df = DataFrame ({"a" : ["a" , "b" , "c" ]})
243+ arr = get_array (df , "a" )
244+ df .replace (["c" ], value , inplace = True )
245+ if using_copy_on_write or value is None :
246+ assert np .shares_memory (arr , get_array (df , "a" ))
247+ else :
248+ # This could be inplace
249+ assert not np .shares_memory (arr , get_array (df , "a" ))
250+ if using_copy_on_write :
251+ assert df ._mgr ._has_no_reference (0 )
252+
253+
254+ def test_replace_list_multiple_elements_inplace (using_copy_on_write ):
255+ df = DataFrame ({"a" : [1 , 2 , 3 ]})
256+ arr = get_array (df , "a" )
257+ df .replace ([1 , 2 ], 4 , inplace = True )
258+ if using_copy_on_write :
259+ # TODO(CoW): This should share memory
260+ assert not np .shares_memory (arr , get_array (df , "a" ))
261+ assert df ._mgr ._has_no_reference (0 )
262+ else :
263+ assert np .shares_memory (arr , get_array (df , "a" ))
264+
265+
266+ def test_replace_list_none (using_copy_on_write ):
267+ df = DataFrame ({"a" : ["a" , "b" , "c" ]})
268+
269+ df_orig = df .copy ()
270+ df2 = df .replace (["b" ], value = None )
271+ tm .assert_frame_equal (df , df_orig )
272+
273+ assert not np .shares_memory (get_array (df , "a" ), get_array (df2 , "a" ))
274+
275+
276+ def test_replace_list_none_inplace_refs (using_copy_on_write ):
277+ df = DataFrame ({"a" : ["a" , "b" , "c" ]})
278+ arr = get_array (df , "a" )
279+ df_orig = df .copy ()
280+ view = df [:]
281+ df .replace (["a" ], value = None , inplace = True )
282+ if using_copy_on_write :
283+ assert df ._mgr ._has_no_reference (0 )
284+ assert not np .shares_memory (arr , get_array (df , "a" ))
285+ tm .assert_frame_equal (df_orig , view )
286+ else :
287+ assert np .shares_memory (arr , get_array (df , "a" ))
0 commit comments