Skip to content

Commit 36dcf51

Browse files
authored
ENH/TST: expand copy-on-write to assign() method (#50010)
1 parent 65047d6 commit 36dcf51

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4887,7 +4887,7 @@ def assign(self, **kwargs) -> DataFrame:
48874887
Portland 17.0 62.6 290.15
48884888
Berkeley 25.0 77.0 298.15
48894889
"""
4890-
data = self.copy()
4890+
data = self.copy(deep=None)
48914891

48924892
for k, v in kwargs.items():
48934893
data[k] = com.apply_if_callable(v, data)

pandas/tests/copy_view/test_methods.py

+18
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,21 @@ def test_head_tail(method, using_copy_on_write):
280280
# without CoW enabled, head and tail return views. Mutating df2 also mutates df.
281281
df2.iloc[0, 0] = 1
282282
tm.assert_frame_equal(df, df_orig)
283+
284+
285+
def test_assign(using_copy_on_write):
286+
df = DataFrame({"a": [1, 2, 3]})
287+
df_orig = df.copy()
288+
df2 = df.assign()
289+
df2._mgr._verify_integrity()
290+
291+
if using_copy_on_write:
292+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
293+
else:
294+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
295+
296+
# modify df2 to trigger CoW for that block
297+
df2.iloc[0, 0] = 0
298+
if using_copy_on_write:
299+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
300+
tm.assert_frame_equal(df, df_orig)

0 commit comments

Comments
 (0)