@@ -253,6 +253,45 @@ def test_set_index(using_copy_on_write):
253
253
tm .assert_frame_equal (df , df_orig )
254
254
255
255
256
+ def test_add_prefix (using_copy_on_write ):
257
+ # GH 49473
258
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
259
+ df_orig = df .copy ()
260
+ df2 = df .add_prefix ("CoW_" )
261
+
262
+ if using_copy_on_write :
263
+ assert np .shares_memory (get_array (df2 , "CoW_a" ), get_array (df , "a" ))
264
+ df2 .iloc [0 , 0 ] = 0
265
+
266
+ assert not np .shares_memory (get_array (df2 , "CoW_a" ), get_array (df , "a" ))
267
+
268
+ if using_copy_on_write :
269
+ assert np .shares_memory (get_array (df2 , "CoW_c" ), get_array (df , "c" ))
270
+ expected = DataFrame (
271
+ {"CoW_a" : [0 , 2 , 3 ], "CoW_b" : [4 , 5 , 6 ], "CoW_c" : [0.1 , 0.2 , 0.3 ]}
272
+ )
273
+ tm .assert_frame_equal (df2 , expected )
274
+ tm .assert_frame_equal (df , df_orig )
275
+
276
+
277
+ def test_add_suffix (using_copy_on_write ):
278
+ # GH 49473
279
+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
280
+ df_orig = df .copy ()
281
+ df2 = df .add_suffix ("_CoW" )
282
+ if using_copy_on_write :
283
+ assert np .shares_memory (get_array (df2 , "a_CoW" ), get_array (df , "a" ))
284
+ df2 .iloc [0 , 0 ] = 0
285
+ assert not np .shares_memory (get_array (df2 , "a_CoW" ), get_array (df , "a" ))
286
+ if using_copy_on_write :
287
+ assert np .shares_memory (get_array (df2 , "c_CoW" ), get_array (df , "c" ))
288
+ expected = DataFrame (
289
+ {"a_CoW" : [0 , 2 , 3 ], "b_CoW" : [4 , 5 , 6 ], "c_CoW" : [0.1 , 0.2 , 0.3 ]}
290
+ )
291
+ tm .assert_frame_equal (df2 , expected )
292
+ tm .assert_frame_equal (df , df_orig )
293
+
294
+
256
295
@pytest .mark .parametrize (
257
296
"method" ,
258
297
[
0 commit comments