Skip to content

Commit 9211826

Browse files
DOC: more explicit note about upcoming CoW changes in copy() method docstring (#56316)
1 parent 3cf05db commit 9211826

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pandas/core/generic.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6686,6 +6686,21 @@ def copy(self, deep: bool_t | None = True) -> Self:
66866686
and index are copied). Any changes to the data of the original
66876687
will be reflected in the shallow copy (and vice versa).
66886688
6689+
.. note::
6690+
The ``deep=False`` behaviour as described above will change
6691+
in pandas 3.0. `Copy-on-Write
6692+
<https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
6693+
will be enabled by default, which means that the "shallow" copy
6694+
is that is returned with ``deep=False`` will still avoid making
6695+
an eager copy, but changes to the data of the original will *no*
6696+
longer be reflected in the shallow copy (or vice versa). Instead,
6697+
it makes use of a lazy (deferred) copy mechanism that will copy
6698+
the data only when any changes to the original or shallow copy is
6699+
made.
6700+
6701+
You can already get the future behavior and improvements through
6702+
enabling copy on write ``pd.options.mode.copy_on_write = True``
6703+
66896704
Parameters
66906705
----------
66916706
deep : bool, default True
@@ -6755,7 +6770,8 @@ def copy(self, deep: bool_t | None = True) -> Self:
67556770
False
67566771
67576772
Updates to the data shared by shallow copy and original is reflected
6758-
in both; deep copy remains unchanged.
6773+
in both (NOTE: this will no longer be true for pandas >= 3.0);
6774+
deep copy remains unchanged.
67596775
67606776
>>> s.iloc[0] = 3
67616777
>>> shallow.iloc[1] = 4
@@ -6788,7 +6804,8 @@ def copy(self, deep: bool_t | None = True) -> Self:
67886804
1 [3, 4]
67896805
dtype: object
67906806
6791-
** Copy-on-Write is set to true: **
6807+
**Copy-on-Write is set to true**, the shallow copy is not modified
6808+
when the original data is changed:
67926809
67936810
>>> with pd.option_context("mode.copy_on_write", True):
67946811
... s = pd.Series([1, 2], index=["a", "b"])

0 commit comments

Comments
 (0)