pythonpython-3.xpandas-settingwithcopy-warning

SettingWithCopyWarning using loc/iloc doesn't set values


sp_eur.iloc[72][['Open_eur','High_eur','Low_eur','Close_eur','Volume_eur','Dividends_eur','Stock Splits_eur']] = sp_eur.iloc[71][['Open_eur','High_eur','Low_eur','Close_eur','Volume_eur','Dividends_eur','Stock Splits_eur']]

returns this:

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  iloc._setitem_with_indexer(indexer, value)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/series.py:1064: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

More important than the warning is the fact that the operations is not performed and even more important is that the code worked perfectly fine before I restarted the kernel.


Solution

  • The problem here was the slicing of the df and the loc syntax. Here's the proper code:

    sp_eur.iloc[72,['Open_eur','High_eur','Low_eur','Close_eur','Volume_eur','Dividends_eur','Stock Splits_eur']] =\ 
    sp_eur.iloc[71,['Open_eur','High_eur','Low_eur','Close_eur','Volume_eur','Dividends_eur','Stock Splits_eur']]