Skip to content

Commit 69ee05f

Browse files
DOC: add deprecation of chained assignment to 2.2 whatsnew (#56403)
1 parent 64c20dc commit 69ee05f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

doc/source/whatsnew/v2.2.0.rst

+64
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,70 @@ Other API changes
451451
Deprecations
452452
~~~~~~~~~~~~
453453

454+
Chained assignment
455+
^^^^^^^^^^^^^^^^^^
456+
457+
In preparation of larger upcoming changes to the copy / view behaviour in pandas 3.0
458+
(:ref:`copy_on_write`, PDEP-7), we started deprecating *chained assignment*.
459+
460+
Chained assignment occurs when you try to update a pandas DataFrame or Series through
461+
two subsequent indexing operations. Depending on the type and order of those operations
462+
this currently does or does not work.
463+
464+
A typical example is as follows:
465+
466+
.. code-block:: python
467+
468+
df = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
469+
470+
# first selecting rows with a mask, then assigning values to a column
471+
# -> this has never worked and raises a SettingWithCopyWarning
472+
df[df["bar"] > 5]["foo"] = 100
473+
474+
# first selecting the column, and then assigning to a subset of that column
475+
# -> this currently works
476+
df["foo"][df["bar"] > 5] = 100
477+
478+
This second example of chained assignment currently works to update the original ``df``.
479+
This will no longer work in pandas 3.0, and therefore we started deprecating this:
480+
481+
.. code-block:: python
482+
483+
>>> df["foo"][df["bar"] > 5] = 100
484+
FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
485+
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
486+
A typical example is when you are setting values in a column of a DataFrame, like:
487+
488+
df["col"][row_indexer] = value
489+
490+
Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.
491+
492+
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
493+
494+
You can fix this warning and ensure your code is ready for pandas 3.0 by removing
495+
the usage of chained assignment. Typically, this can be done by doing the assignment
496+
in a single step using for example ``.loc``. For the example above, we can do:
497+
498+
.. code-block:: python
499+
500+
df.loc[df["bar"] > 5, "foo"] = 100
501+
502+
The same deprecation applies to inplace methods that are done in a chained manner, such as:
503+
504+
.. code-block:: python
505+
506+
>>> df["foo"].fillna(0, inplace=True)
507+
FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
508+
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
509+
510+
For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.
511+
512+
When the goal is to update the column in the DataFrame ``df``, the alternative here is
513+
to call the method on ``df`` itself, such as ``df.fillna({"foo": 0}, inplace=True)``.
514+
515+
See more details in the :ref:`migration guide <copy_on_write.migration_guide>`.
516+
517+
454518
Deprecate aliases ``M``, ``Q``, ``Y``, etc. in favour of ``ME``, ``QE``, ``YE``, etc. for offsets
455519
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
456520

0 commit comments

Comments
 (0)