Skip to content

Commit

Permalink
CoW: Clean up some copy usage (pandas-dev#57513)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and pmhatre1 committed May 7, 2024
1 parent 94faf90 commit dc7ac67
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
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 @@ -10279,7 +10279,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

0 comments on commit dc7ac67

Please sign in to comment.