Skip to content

Commit

Permalink
DOC: Add note to user guide that SettingWithCopyWarning won't be nece…
Browse files Browse the repository at this point in the history
…ssary anymore (#56032)
  • Loading branch information
phofl authored Nov 20, 2023
1 parent e7fa8bb commit be08de8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/source/user_guide/copy_on_write.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
Copy-on-Write (CoW)
*******************

.. note::

Copy-on-Write will become the default in pandas 3.0. We recommend
:ref:`turning it on now <copy_on_write_enabling>`
to benefit from all improvements.

Copy-on-Write was first introduced in version 1.5.0. Starting from version 2.0 most of the
optimizations that become possible through CoW are implemented and supported. All possible
optimizations are supported starting from pandas 2.1.
Expand Down Expand Up @@ -123,6 +129,8 @@ CoW triggers a copy when ``df`` is changed to avoid mutating ``view`` as well:
df
view
.. _copy_on_write_chained_assignment:

Chained Assignment
------------------

Expand Down Expand Up @@ -238,6 +246,8 @@ and :meth:`DataFrame.rename`.
These methods return views when Copy-on-Write is enabled, which provides a significant
performance improvement compared to the regular execution.

.. _copy_on_write_enabling:

How to enable CoW
-----------------

Expand Down
48 changes: 48 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,22 @@ You can assign a custom index to the ``index`` attribute:
Returning a view versus a copy
------------------------------

.. warning::

:ref:`Copy-on-Write <copy_on_write>`
will become the new default in pandas 3.0. This means than chained indexing will
never work. As a consequence, the ``SettingWithCopyWarning`` won't be necessary
anymore.
See :ref:`this section <copy_on_write_chained_assignment>`
for more context.
We recommend turning Copy-on-Write on to leverage the improvements with

```
pd.options.mode.copy_on_write = True
```

even before pandas 3.0 is available.

When setting values in a pandas object, care must be taken to avoid what is called
``chained indexing``. Here is an example.

Expand Down Expand Up @@ -1765,6 +1781,22 @@ faster, and allows one to index *both* axes if so desired.
Why does assignment fail when using chained indexing?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. warning::

:ref:`Copy-on-Write <copy_on_write>`
will become the new default in pandas 3.0. This means than chained indexing will
never work. As a consequence, the ``SettingWithCopyWarning`` won't be necessary
anymore.
See :ref:`this section <copy_on_write_chained_assignment>`
for more context.
We recommend turning Copy-on-Write on to leverage the improvements with

```
pd.options.mode.copy_on_write = True
```

even before pandas 3.0 is available.

The problem in the previous section is just a performance issue. What's up with
the ``SettingWithCopy`` warning? We don't **usually** throw warnings around when
you do something that might cost a few extra milliseconds!
Expand Down Expand Up @@ -1821,6 +1853,22 @@ Yikes!
Evaluation order matters
~~~~~~~~~~~~~~~~~~~~~~~~

.. warning::

:ref:`Copy-on-Write <copy_on_write>`
will become the new default in pandas 3.0. This means than chained indexing will
never work. As a consequence, the ``SettingWithCopyWarning`` won't be necessary
anymore.
See :ref:`this section <copy_on_write_chained_assignment>`
for more context.
We recommend turning Copy-on-Write on to leverage the improvements with

```
pd.options.mode.copy_on_write = True
```

even before pandas 3.0 is available.

When you use chained indexing, the order and type of the indexing operation
partially determine whether the result is a slice into the original object, or
a copy of the slice.
Expand Down

0 comments on commit be08de8

Please sign in to comment.