You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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
+
454
518
Deprecate aliases ``M``, ``Q``, ``Y``, etc. in favour of ``ME``, ``QE``, ``YE``, etc. for offsets
0 commit comments