0

I am trying to get rid of the SettingWithCopyWarning.

my code :

combineQueryandBookFiltered['positionId'] = combineQueryandBookFiltered['positionId'].astype('int64')

combineQueryandBookFiltered.loc['pnlValue'] = np.multiply(combineQueryandBookFiltered['pnlValue'], df_fxrate['fx_rate'])

what I tried:

combineQueryandBookFiltered['positionId'] = combineQueryandBookFiltered['positionId'].astype('int64').copy()

combineQueryandBookFiltered.loc['pnlValue'] = np.multiply(combineQueryandBookFiltered['pnlValue'], df_fxrate['fx_rate'])

console message:

//stack/over/flow/code1.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  combineQueryandBookFiltered['positionId'] = combineQueryandBookFiltered['positionId'].astype('int64').copy()
//stack/over/flow/code1:156: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  combineQueryandBookFiltered.loc['pnlValue'] = np.multiply(combineQueryandBookFiltered['pnlValue'], df_fxrate['fx_rate'])

can anyone help eliminate these messages?

excelguy
  • 1,574
  • 6
  • 33
  • 67
  • Your `combineQueryandBookFiltered` is a slice of another dataframe. Update that dataframe if it's really what you want. Else do a copy operation: `combineQueryandBookFiltered = otherdf[condition].copy()` – Quang Hoang Sep 30 '19 at 18:48
  • I thought I did a `.copy()` above. then it goes into the next line. Is this correct? – excelguy Sep 30 '19 at 20:14
  • No, add `copy()` when you **first created** the `combineQueryandBookFiltered` dataframe. – Quang Hoang Sep 30 '19 at 20:15

1 Answers1

0

You can try running this at the beginning of your code, right after you import pandas:

pd.options.mode.chained_assignment = None

However, it is important to understand the reason and work it out, instead of simply supressing the warning. You can find a full explanation here

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53