Skip to content

ENH/TST: expand copy-on-write to assign() method #50010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4887,7 +4887,7 @@ def assign(self, **kwargs) -> DataFrame:
Portland 17.0 62.6 290.15
Berkeley 25.0 77.0 298.15
"""
data = self.copy()
data = self.copy(deep=None)

for k, v in kwargs.items():
data[k] = com.apply_if_callable(v, data)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,21 @@ def test_head_tail(method, using_copy_on_write):
# without CoW enabled, head and tail return views. Mutating df2 also mutates df.
df2.iloc[0, 0] = 1
tm.assert_frame_equal(df, df_orig)


def test_assign(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
df2 = df.assign()
df2._mgr._verify_integrity()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
df2._mgr._verify_integrity()

For future tests, you can leave out this part (I don't think it is needed to repeat in every test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will do.


if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

# modify df2 to trigger CoW for that block
df2.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)