Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: more explicit note about upcoming CoW changes in copy() method docstring #56316

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6681,6 +6681,21 @@ def copy(self, deep: bool_t | None = True) -> Self:
and index are copied). Any changes to the data of the original
will be reflected in the shallow copy (and vice versa).

.. note::
The ``deep=False`` behaviour as described above will change
in pandas 3.0. `Copy-on-Write
<https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
will be enabled by default, which means that the "shallow" copy
is that is returned with ``deep=False`` will still avoid making
an eager copy, but changes to the data of the original will *no*
longer be reflected in the shallow copy (or vice versa). Instead,
it makes use of a lazy (deferred) copy mechanism that will copy
the data only when any changes to the original or shallow copy is
made.

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

Parameters
----------
deep : bool, default True
Expand Down Expand Up @@ -6750,7 +6765,8 @@ def copy(self, deep: bool_t | None = True) -> Self:
False

Updates to the data shared by shallow copy and original is reflected
in both; deep copy remains unchanged.
in both (NOTE: this will no longer be true for pandas >= 3.0);
deep copy remains unchanged.

>>> s.iloc[0] = 3
>>> shallow.iloc[1] = 4
Expand Down Expand Up @@ -6783,7 +6799,8 @@ def copy(self, deep: bool_t | None = True) -> Self:
1 [3, 4]
dtype: object

** Copy-on-Write is set to true: **
**Copy-on-Write is set to true**, the shallow copy is not modified
when the original data is changed:

>>> with pd.option_context("mode.copy_on_write", True):
... s = pd.Series([1, 2], index=["a", "b"])
Expand Down
Loading