Skip to content

CoW: Clean up some copy usage #57513

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
Feb 19, 2024
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/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def eval(
try:
target = env.target
if isinstance(target, NDFrame):
target = target.copy(deep=None)
target = target.copy(deep=False)
else:
target = target.copy()
except AttributeError as err:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7488,7 +7488,7 @@ def replace(
if not self.size:
if inplace:
return None
return self.copy(deep=None)
return self.copy(deep=False)

if is_dict_like(to_replace):
if is_dict_like(value): # {'A' : NA} -> {'A' : 0}
Expand Down Expand Up @@ -10281,7 +10281,7 @@ def shift(
fill_value = lib.no_default

if periods == 0:
return self.copy(deep=None)
return self.copy(deep=False)

if is_list_like(periods) and isinstance(self, ABCSeries):
return self.to_frame().shift(
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _combine(self, blocks: list[Block], index: Index | None = None) -> Self:
def nblocks(self) -> int:
return len(self.blocks)

def copy(self, deep: bool | None | Literal["all"] = True) -> Self:
def copy(self, deep: bool | Literal["all"] = True) -> Self:
"""
Make deep or shallow copy of BlockManager

Expand All @@ -716,7 +716,6 @@ def copy(self, deep: bool | None | Literal["all"] = True) -> Self:
-------
BlockManager
"""
deep = deep if deep is not None else False
# this preserves the notion of view copying of axes
if deep:
# hit in e.g. tests.io.json.test_pandas
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/copy_view/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_iset_splits_blocks_inplace(locs, arr, dtype):
)
arr = arr.astype(dtype)
df_orig = df.copy()
df2 = df.copy(deep=None) # Trigger a CoW (if enabled, otherwise makes copy)
df2 = df.copy(deep=False) # Trigger a CoW (if enabled, otherwise makes copy)
df2._mgr.iset(locs, arr, inplace=True)

tm.assert_frame_equal(df, df_orig)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ def test_interpolate_creates_copy():
def test_isetitem():
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df_orig = df.copy()
df2 = df.copy(deep=None) # Trigger a CoW
df2 = df.copy(deep=False) # Trigger a CoW
df2.isetitem(1, np.array([-1, -2, -3])) # This is inplace
assert np.shares_memory(get_array(df, "c"), get_array(df2, "c"))
assert np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
Expand Down