@@ -47,6 +47,51 @@ def test_replace(using_copy_on_write, replace_kwargs):
47
47
tm .assert_frame_equal (df , df_orig )
48
48
49
49
50
+ def test_replace_regex_inplace_refs (using_copy_on_write ):
51
+ df = DataFrame ({"a" : ["aaa" , "bbb" ]})
52
+ df_orig = df .copy ()
53
+ view = df [:]
54
+ arr = get_array (df , "a" )
55
+ df .replace (to_replace = r"^a.*$" , value = "new" , inplace = True , regex = True )
56
+ if using_copy_on_write :
57
+ assert not np .shares_memory (arr , get_array (df , "a" ))
58
+ assert df ._mgr ._has_no_reference (0 )
59
+ tm .assert_frame_equal (view , df_orig )
60
+ else :
61
+ assert np .shares_memory (arr , get_array (df , "a" ))
62
+
63
+
64
+ def test_replace_regex_inplace (using_copy_on_write ):
65
+ df = DataFrame ({"a" : ["aaa" , "bbb" ]})
66
+ arr = get_array (df , "a" )
67
+ df .replace (to_replace = r"^a.*$" , value = "new" , inplace = True , regex = True )
68
+ if using_copy_on_write :
69
+ assert df ._mgr ._has_no_reference (0 )
70
+ assert np .shares_memory (arr , get_array (df , "a" ))
71
+
72
+ df_orig = df .copy ()
73
+ df2 = df .replace (to_replace = r"^b.*$" , value = "new" , regex = True )
74
+ tm .assert_frame_equal (df_orig , df )
75
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
76
+
77
+
78
+ def test_replace_regex_inplace_no_op (using_copy_on_write ):
79
+ df = DataFrame ({"a" : [1 , 2 ]})
80
+ arr = get_array (df , "a" )
81
+ df .replace (to_replace = r"^a.$" , value = "new" , inplace = True , regex = True )
82
+ if using_copy_on_write :
83
+ assert df ._mgr ._has_no_reference (0 )
84
+ assert np .shares_memory (arr , get_array (df , "a" ))
85
+
86
+ df_orig = df .copy ()
87
+ df2 = df .replace (to_replace = r"^x.$" , value = "new" , regex = True )
88
+ tm .assert_frame_equal (df_orig , df )
89
+ if using_copy_on_write :
90
+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
91
+ else :
92
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
93
+
94
+
50
95
def test_replace_mask_all_false_second_block (using_copy_on_write ):
51
96
df = DataFrame ({"a" : [1.5 , 2 , 3 ], "b" : 100.5 , "c" : 1 , "d" : 2 })
52
97
df_orig = df .copy ()
0 commit comments