首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SettingWithCopyWarning,使用loc

SettingWithCopyWarning,使用loc
EN

Stack Overflow用户
提问于 2019-10-01 02:04:56
回答 1查看 26关注 0票数 0

我正在试着摆脱SettingWithCopyWarning

我的代码:

代码语言:javascript
复制
combineQueryandBookFiltered['positionId'] = combineQueryandBookFiltered['positionId'].astype('int64')

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

我尝试过的:

代码语言:javascript
复制
combineQueryandBookFiltered['positionId'] = combineQueryandBookFiltered['positionId'].astype('int64').copy()

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

控制台消息:

代码语言:javascript
复制
//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'])

有人可以帮助消除这些消息吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-01 02:10:33

在导入pandas之后,您可以尝试在代码的开头运行此代码:

代码语言:javascript
复制
pd.options.mode.chained_assignment = None

然而,重要的是理解原因并解决它,而不是简单地抑制警告。你可以找到完整的解释here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58173045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档