Skip to content

Commit f918172

Browse files
authored
BUG: copy.copy returning deep instead of shallow copy (#62971)
1 parent 09a83d7 commit f918172

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
@@ -1278,6 +1278,7 @@ Other
12781278
- Bug in ``divmod`` and ``rdivmod`` with :class:`DataFrame`, :class:`Series`, and :class:`Index` with ``bool`` dtypes failing to raise, which was inconsistent with ``__floordiv__`` behavior (:issue:`46043`)
12791279
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
12801280
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
1281+
- 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:`62971`)
12811282
- Deprecated the keyword ``check_datetimelike_compat`` in :meth:`testing.assert_frame_equal` and :meth:`testing.assert_series_equal` (:issue:`55638`)
12821283
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when trying to replace :class:`NA` values in a :class:`Float64Dtype` object with ``np.nan``; this now works with ``pd.set_option("mode.nan_is_na", False)`` and is irrelevant otherwise (:issue:`55127`)
12831284
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when trying to replace :class:`np.nan` values in a :class:`Int64Dtype` object with :class:`NA`; this is now a no-op with ``pd.set_option("mode.nan_is_na", False)`` and is irrelevant otherwise (:issue:`51237`)

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)