@@ -6704,8 +6704,7 @@ def copy(self, deep: bool_t | None = True) -> Self:
6704
6704
:ref:`gotchas <gotchas.thread-safety>` when copying in a threading
6705
6705
environment.
6706
6706
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.
6709
6708
This means that any changes to the copied data would make a new copy
6710
6709
of the data upon write (and vice versa). Changes made to either the
6711
6710
original or copied variable would not be reflected in the counterpart.
@@ -6731,12 +6730,15 @@ def copy(self, deep: bool_t | None = True) -> Self:
6731
6730
>>> deep = s.copy()
6732
6731
>>> shallow = s.copy(deep=False)
6733
6732
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.
6735
6735
6736
6736
>>> s is shallow
6737
6737
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
6740
6742
6741
6743
Deep copy has own copy of data and index.
6742
6744
@@ -6745,18 +6747,17 @@ def copy(self, deep: bool_t | None = True) -> Self:
6745
6747
>>> s.values is deep.values or s.index is deep.index
6746
6748
False
6747
6749
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.
6751
6752
6752
6753
>>> s.iloc[0] = 3
6753
6754
>>> shallow.iloc[1] = 4
6754
6755
>>> s
6755
6756
a 3
6756
- b 4
6757
+ b 2
6757
6758
dtype: int64
6758
6759
>>> shallow
6759
- a 3
6760
+ a 1
6760
6761
b 4
6761
6762
dtype: int64
6762
6763
>>> deep
@@ -6779,22 +6780,6 @@ def copy(self, deep: bool_t | None = True) -> Self:
6779
6780
0 [10, 2]
6780
6781
1 [3, 4]
6781
6782
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
6798
6783
"""
6799
6784
data = self ._mgr .copy (deep = deep )
6800
6785
self ._clear_item_cache ()
0 commit comments