使用Loc的SettingWithCopyWarning

时间:2019-09-30 18:04:55

标签: python-3.x pandas

我正试图摆脱SettingWithCopyWarning

我的代码:

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

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

我尝试了什么:

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

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

控制台消息:

//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'])

任何人都可以帮助消除这些消息吗?

1 个答案:

答案 0 :(得分:0)

您可以在导入熊猫后立即在代码开头尝试运行此代码:

pd.options.mode.chained_assignment = None

但是,重要的是理解原因并加以解决,而不是简单地抑制警告。您可以找到完整的说明here