Skip to content

Commit 9d7bfa5

Browse files
committed
add tests for series axis
1 parent 84261bc commit 9d7bfa5

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pandas/tests/copy_view/test_methods.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,34 @@ def test_chained_methods(request, method, idx, using_copy_on_write):
235235
tm.assert_frame_equal(df2.iloc[:, idx:], df_orig)
236236

237237

238-
def test_set_index(using_copy_on_write):
238+
def test_set_frame_axis(using_copy_on_write):
239239
# GH 49473
240240
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
241241
df_orig = df.copy()
242-
df2 = df.set_index("a")
242+
df2 = df.set_axis(["a", "b", "c"], axis="index")
243243

244244
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"))
246246
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"))
248248

249249
# mutating df2 triggers a copy-on-write for that column / block
250250
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"))
252252
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

Comments
 (0)