Skip to content

Commit de694e3

Browse files
committed
BUG: copy.copy returning deep instead of shallow copy
1 parent c3bace8 commit de694e3

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ Other
12651265
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` throwing ``ValueError`` when ``regex=True`` and all NA values. (:issue:`60688`)
12661266
- Bug in :meth:`Series.to_string` when series contains complex floats with exponents (:issue:`60405`)
12671267
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
1268+
- Bug when calling :py:func:`copy.copy` on a :class:`DataFrame` or :class:`Series` which would return a deep copy instead of a shallow copy (:issue:`?`)
12681269
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
12691270
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
12701271
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)

pandas/core/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6680,8 +6680,8 @@ def copy(self, deep: bool = True) -> Self:
66806680
)
66816681

66826682
@final
6683-
def __copy__(self, deep: bool = True) -> Self:
6684-
return self.copy(deep=deep)
6683+
def __copy__(self) -> Self:
6684+
return self.copy(deep=False)
66856685

66866686
@final
66876687
def __deepcopy__(self, memo=None) -> Self:

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,8 @@ def copy(
14131413
return new_index
14141414

14151415
@final
1416-
def __copy__(self, **kwargs) -> Self:
1417-
return self.copy(**kwargs)
1416+
def __copy__(self) -> Self:
1417+
return self.copy(deep=False)
14181418

14191419
@final
14201420
def __deepcopy__(self, memo=None) -> Self:

pandas/tests/generic/test_generic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ def test_copy_and_deepcopy(self, frame_or_series, shape, func):
307307
assert obj_copy is not obj
308308
tm.assert_equal(obj_copy, obj)
309309

310+
def test_stdlib_copy_shallow_copies(self, frame_or_series):
311+
obj = frame_or_series(range(3))
312+
obj_copy = copy(obj)
313+
assert tm.shares_memory(obj, obj_copy)
314+
310315

311316
class TestNDFrame:
312317
# tests that don't fit elsewhere

0 commit comments

Comments
 (0)