-2

I tried many times, but I couldn´t avoid this warning in my code, when inserting the 'multiplier' column on 'ipcaMomSlice'. Any idea?

import pandas as pd

ipcaMom = bcbQuery(433)
ipcaMom['valor'] /= 100

initDate = "1995-01-01"

ipcaMomSlice = ipcaMom[initDate:]

ipcaMomSlice.loc[:,'multiplier'] = (1 + ipcaMomSlice['valor']).cumprod()

AMC
  • 2,642
  • 7
  • 13
  • 35
Daniel Arges
  • 345
  • 3
  • 13
  • Why not create a column the normal way like this: `ipcaMomSlice['multiplier'] = (1 + ipcaMomSlice['valor']).cumprod()` ? – David Erickson Oct 02 '20 at 23:26
  • 3
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – AMC Oct 03 '20 at 00:14
  • Please provide the entire error output. – AMC Oct 03 '20 at 00:14

1 Answers1

1

If you want this ipcaMomSlice to be it's own entity, and not refer back to ipcaMom (e.g. you don't want to assign a "multiplier" column to ipcaMom at all, and only want the "multiplier" on ipcaMomSlice) you'll need to tell pandas that ipcaMomSlice is no longer just a slice of a dataframe, but a full on independent subset. This is done with the .copy() method.

import pandas as pd

ipcaMom = bcbQuery(433)
ipcaMom['valor'] /= 100

initDate = "1995-01-01"

ipcaMomSlice = ipcaMom[initDate:].copy()

# no need for `.loc` in this assignment operation
ipcaMomSlice['multiplier'] = (1 + ipcaMomSlice['valor']).cumprod()

If however you're expecting "multiplier" to magically appear as a column in ipcaMom you'll need to combine your indexing from your .loc

import pandas as pd

ipcaMom = bcbQuery(433)
ipcaMom['valor'] /= 100

initDate = "1995-01-01"
ipcaMom.loc[initdate:, 'multiplier'] = (1 + ipcaMom.loc[initdate:, 'valor']).cumprod()
Cameron Riddell
  • 10,942
  • 9
  • 19
  • @DanielArges Would you mind selecting my answer as correct if this worked for you so others can see this is an answered issue in the future? – Cameron Riddell Oct 05 '20 at 17:44