@@ -6704,8 +6704,7 @@ def copy(self, deep: bool_t | None = True) -> Self:
67046704 :ref:`gotchas <gotchas.thread-safety>` when copying in a threading
67056705 environment.
67066706
6707- When ``copy_on_write`` in pandas config is set to ``True``, the
6708- ``copy_on_write`` config takes effect even when ``deep=False``.
6707+ Copy-on-Write protects shallow copies against accidental modifications.
67096708 This means that any changes to the copied data would make a new copy
67106709 of the data upon write (and vice versa). Changes made to either the
67116710 original or copied variable would not be reflected in the counterpart.
@@ -6731,12 +6730,15 @@ def copy(self, deep: bool_t | None = True) -> Self:
67316730 >>> deep = s.copy()
67326731 >>> shallow = s.copy(deep=False)
67336732
6734- Shallow copy shares data and index with original.
6733+ Shallow copy shares index with original, the data is a
6734+ view of the original.
67356735
67366736 >>> s is shallow
67376737 False
6738- >>> s.values is shallow.values and s.index is shallow.index
6739- True
6738+ >>> s.values is shallow.values
6739+ False
6740+ >>> s.index is shallow.index
6741+ False
67406742
67416743 Deep copy has own copy of data and index.
67426744
@@ -6745,18 +6747,17 @@ def copy(self, deep: bool_t | None = True) -> Self:
67456747 >>> s.values is deep.values or s.index is deep.index
67466748 False
67476749
6748- Updates to the data shared by shallow copy and original is reflected
6749- in both (NOTE: this will no longer be true for pandas >= 3.0);
6750- deep copy remains unchanged.
6750+ The shallow copy is protected against updating the original object
6751+ as well. Thus, updates will only reflect in one of both objects.
67516752
67526753 >>> s.iloc[0] = 3
67536754 >>> shallow.iloc[1] = 4
67546755 >>> s
67556756 a 3
6756- b 4
6757+ b 2
67576758 dtype: int64
67586759 >>> shallow
6759- a 3
6760+ a 1
67606761 b 4
67616762 dtype: int64
67626763 >>> deep
@@ -6779,22 +6780,6 @@ def copy(self, deep: bool_t | None = True) -> Self:
67796780 0 [10, 2]
67806781 1 [3, 4]
67816782 dtype: object
6782-
6783- **Copy-on-Write is set to true**, the shallow copy is not modified
6784- when the original data is changed:
6785-
6786- >>> with pd.option_context("mode.copy_on_write", True):
6787- ... s = pd.Series([1, 2], index=["a", "b"])
6788- ... copy = s.copy(deep=False)
6789- ... s.iloc[0] = 100
6790- ... s
6791- a 100
6792- b 2
6793- dtype: int64
6794- >>> copy
6795- a 1
6796- b 2
6797- dtype: int64
67986783 """
67996784 data = self ._mgr .copy (deep = deep )
68006785 self ._clear_item_cache ()
0 commit comments