Skip to content

Commit 2ad6f8b

Browse files
committed
chanage copy default to None and test CoW in set_axis
1 parent 428c2a6 commit 2ad6f8b

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4942,7 +4942,7 @@ def set_axis(
49424942
labels,
49434943
*,
49444944
axis: Axis = 0,
4945-
copy: bool = True,
4945+
copy: bool = None,
49464946
) -> DataFrame:
49474947
return super().set_axis(labels, axis=axis, copy=copy)
49484948

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def set_axis(
700700
labels,
701701
*,
702702
axis: Axis = 0,
703-
copy: bool_t = True,
703+
copy: bool_t = None,
704704
) -> NDFrameT:
705705
"""
706706
Assign desired index to given axis.

pandas/tests/copy_view/test_methods.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,37 @@ def test_reorder_levels(using_copy_on_write):
356356
if using_copy_on_write:
357357
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
358358
tm.assert_frame_equal(df, df_orig)
359+
360+
361+
def test_frame_set_axis(using_copy_on_write):
362+
# GH 49473
363+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
364+
df_orig = df.copy()
365+
df2 = df.set_axis(["a", "b", "c"], axis="index")
366+
367+
if using_copy_on_write:
368+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
369+
else:
370+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
371+
372+
# mutating df2 triggers a copy-on-write for that column / block
373+
df2.iloc[0, 0] = 0
374+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
375+
tm.assert_frame_equal(df, df_orig)
376+
377+
378+
def test_series_set_axis(using_copy_on_write):
379+
# GH 49473
380+
ser = Series([1, 2, 3])
381+
ser_orig = ser.copy()
382+
ser2 = ser.set_axis(["a", "b", "c"], axis="index")
383+
384+
if using_copy_on_write:
385+
assert np.shares_memory(ser, ser2)
386+
else:
387+
assert not np.shares_memory(ser, ser2)
388+
389+
# mutating ser triggers a copy-on-write for the column / block
390+
ser2.iloc[0] = 0
391+
assert not np.shares_memory(ser2, ser)
392+
tm.assert_series_equal(ser, ser_orig)

pandas/tests/frame/methods/test_set_axis.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_set_axis(self, obj):
2121
result = obj.set_axis(new_index, axis=0)
2222
tm.assert_equal(expected, result)
2323

24-
def test_set_axis_copy(self, obj):
24+
def test_set_axis_copy(self, obj, using_copy_on_write):
2525
# Test copy keyword GH#47932
2626
new_index = list("abcd")[: len(obj)]
2727

@@ -32,14 +32,15 @@ def test_set_axis_copy(self, obj):
3232
result = obj.set_axis(new_index, axis=0, copy=True)
3333
tm.assert_equal(expected, result)
3434
assert result is not obj
35-
# check we DID make a copy
36-
if obj.ndim == 1:
37-
assert not tm.shares_memory(result, obj)
38-
else:
39-
assert not any(
40-
tm.shares_memory(result.iloc[:, i], obj.iloc[:, i])
41-
for i in range(obj.shape[1])
42-
)
35+
if not using_copy_on_write:
36+
# check we DID make a copy
37+
if obj.ndim == 1:
38+
assert not tm.shares_memory(result, obj)
39+
else:
40+
assert not any(
41+
tm.shares_memory(result.iloc[:, i], obj.iloc[:, i])
42+
for i in range(obj.shape[1])
43+
)
4344

4445
result = obj.set_axis(new_index, axis=0, copy=False)
4546
tm.assert_equal(expected, result)
@@ -58,13 +59,14 @@ def test_set_axis_copy(self, obj):
5859
tm.assert_equal(expected, result)
5960
assert result is not obj
6061
# check we DID make a copy
61-
if obj.ndim == 1:
62-
assert not tm.shares_memory(result, obj)
63-
else:
64-
assert not any(
65-
tm.shares_memory(result.iloc[:, i], obj.iloc[:, i])
66-
for i in range(obj.shape[1])
67-
)
62+
if not using_copy_on_write:
63+
if obj.ndim == 1:
64+
assert not tm.shares_memory(result, obj)
65+
else:
66+
assert not any(
67+
tm.shares_memory(result.iloc[:, i], obj.iloc[:, i])
68+
for i in range(obj.shape[1])
69+
)
6870

6971
res = obj.set_axis(new_index, copy=False)
7072
tm.assert_equal(expected, res)

0 commit comments

Comments
 (0)