【发布时间】:2017-08-27 04:12:17
【问题描述】:
我一直在寻找阅读 pandas 文档 here 并尝试使用围绕 here 和 here 发布的问题的不同代码行,但我似乎无法摆脱带有复制警告的设置。我更愿意学习以“正确”的方式对其进行编码,而不仅仅是ignoring the warnings.
以下代码行位于 for 循环中,我不想多次生成此警告,因为它可能会减慢速度。
我正在尝试创建一个名称为:'E'+vs 的新列,其中 vs 是 for 循环中列表中的字符串
但是对于它们中的每一个,我仍然收到以下警告,即使是最后 3 行:
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
这是我迄今为止尝试过的麻烦行:
#based on research, the first two seem to be the "wrong" way
df_out['E'+vs] = df_out[kvs].rolling(v).mean().copy()
df_out['E'+vs] = df_out[kvs].rolling(v).mean()
df_out.loc[:,'E'+vs] = df_out[kvs].rolling(v).mean().copy()
df_out.loc[:,'E'+vs] = df_out[kvs].rolling(v).mean()
df_out.loc[:,'E'+vs] = df_out.loc[:,kvs].rolling(v).mean()
另一个给出 SettingWithCopyWarning 的是:
df_out.dropna(inplace=True,axis=0)
这个也发出警告(但我认为这个会)
df_out = df_out.dropna(inplace=True,axis=0)
如何正确执行这两项操作?
编辑:这是生成原始 df_out 的代码
df_out= pd.concat([vol.Date[1:-1], ret.Return_Time[:-2], vol.Freq_Time[:-2],
vol.Freq_Time[:-1].shift(-1), vol.Freq_Time[:].shift(-2)],
axis=1).dropna().set_index('Date')
【问题讨论】:
标签: python python-3.x pandas chained-assignment