@@ -235,18 +235,34 @@ def test_chained_methods(request, method, idx, using_copy_on_write):
235
235
tm .assert_frame_equal (df2 .iloc [:, idx :], df_orig )
236
236
237
237
238
- def test_set_index (using_copy_on_write ):
238
+ def test_set_frame_axis (using_copy_on_write ):
239
239
# GH 49473
240
240
df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]})
241
241
df_orig = df .copy ()
242
- df2 = df .set_index ( "a " )
242
+ df2 = df .set_axis ([ "a" , "b" , "c" ], axis = "index " )
243
243
244
244
if using_copy_on_write :
245
- assert np .shares_memory (get_array (df2 , "b " ), get_array (df , "b " ))
245
+ assert np .shares_memory (get_array (df2 , "a " ), get_array (df , "a " ))
246
246
else :
247
- assert not np .shares_memory (get_array (df2 , "b " ), get_array (df , "b " ))
247
+ assert not np .shares_memory (get_array (df2 , "a " ), get_array (df , "a " ))
248
248
249
249
# mutating df2 triggers a copy-on-write for that column / block
250
250
df2 .iloc [0 , 1 ] = 0
251
- assert not np .shares_memory (get_array (df2 , "c " ), get_array (df , "c " ))
251
+ assert not np .shares_memory (get_array (df2 , "b " ), get_array (df , "b " ))
252
252
tm .assert_frame_equal (df , df_orig )
253
+
254
+ def test_set_series_axis (using_copy_on_write ):
255
+ # GH 49473
256
+ ser = Series ([1 , 2 , 3 ])
257
+ ser_orig = ser .copy ()
258
+ ser = ser .set_axis (["a" , "b" , "c" ], axis = "index" )
259
+
260
+ if using_copy_on_write :
261
+ assert np .shares_memory (get_array (ser , "a" ), get_array (ser , "a" ))
262
+ else :
263
+ assert not np .shares_memory (get_array (ser , "a" ), get_array (ser , "a" ))
264
+
265
+ # mutating ser triggers a copy-on-write for that column / block
266
+ ser .iloc [0 , 0 ] = 0
267
+ assert not np .shares_memory (get_array (ser , "b" ), get_array (ser , "b" ))
268
+ tm .assert_frame_equal (ser , ser_orig )
0 commit comments