Skip to content

Commit 5b85b43

Browse files
TST/CoW: copy-on-write tests for add_prefix and add_suffix (#49991)
1 parent 470fee6 commit 5b85b43

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pandas/tests/copy_view/test_methods.py

+39
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,45 @@ def test_set_index(using_copy_on_write):
253253
tm.assert_frame_equal(df, df_orig)
254254

255255

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+
256295
@pytest.mark.parametrize(
257296
"method",
258297
[

0 commit comments

Comments
 (0)