diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 752d08a526d8c..26a9a6854b8c5 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1275,6 +1275,7 @@ Other - 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`) - Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`) - Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`) +- 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`) - Deprecated the keyword ``check_datetimelike_compat`` in :meth:`testing.assert_frame_equal` and :meth:`testing.assert_series_equal` (:issue:`55638`) - 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`) - 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`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b542ca1f431c3..735a59e5cfbb8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6680,8 +6680,8 @@ def copy(self, deep: bool = True) -> Self: ) @final - def __copy__(self, deep: bool = True) -> Self: - return self.copy(deep=deep) + def __copy__(self) -> Self: + return self.copy(deep=False) @final def __deepcopy__(self, memo=None) -> Self: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 72f7a1e086b60..904de6a012671 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1413,8 +1413,8 @@ def copy( return new_index @final - def __copy__(self, **kwargs) -> Self: - return self.copy(**kwargs) + def __copy__(self) -> Self: + return self.copy(deep=False) @final def __deepcopy__(self, memo=None) -> Self: diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index b591b1b1092d4..ee6503b6929b6 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -307,6 +307,11 @@ def test_copy_and_deepcopy(self, frame_or_series, shape, func): assert obj_copy is not obj tm.assert_equal(obj_copy, obj) + def test_stdlib_copy_shallow_copies(self, frame_or_series): + obj = frame_or_series(range(3)) + obj_copy = copy(obj) + assert tm.shares_memory(obj, obj_copy) + class TestNDFrame: # tests that don't fit elsewhere