Pandas中的.loc[] = value返回SettingWithCopyWarning

3

问题

我用代码获取以下错误消息。据说,问题在于我首先使用.loc对dataframe进行切片,然后尝试向该切片分配值。据我了解,Pandas不能百分之百确定我是要将值分配给仅切片,还是要将其传播回原始df。我不知道如何解决这个问题。

错误消息

C:\blp\BQuant\environments\bqnt-1.25.2\lib\site-packages\pandas\core\indexing.py:140: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)

完整代码

import numpy as np
import pandas as pd
import datetime as dt
import time

csv1 = pd.read_csv('stock_price.csv', delimiter = ',')
df = pd.DataFrame(csv1)

df['delta'] = df.PX_LAST.pct_change()
df.loc[df.index[0], 'avg_gain'] = 0

for x in range(1,len(df.index)):
    if df["delta"].iloc[x] > 0:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1] * 13) + df["delta"].iloc[x]) / 14
    else:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1] * 13) + 0) / 14   

df

输入

Dates,PX_LAST
03/09/2018,157.512
04/09/2018,155.393
05/09/2018,154.069
06/09/2018,155.109
07/09/2018,156.301
10/09/2018,156.717
11/09/2018,157.19
12/09/2018,157.549
13/09/2018,159.157
14/09/2018,158.363
17/09/2018,158.968

输出

Dates,PX_LAST,delta,avg_gain
03/09/2018,157.512,NaN,0
04/09/2018,155.393,-0.013453,0
05/09/2018,154.069,-0.00852,0
06/09/2018,155.109,0.00675,0.000482
07/09/2018,156.301,0.007685,0.000997
10/09/2018,156.717,0.002662,0.001116
11/09/2018,157.19,0.003018,0.001251
12/09/2018,157.549,0.002284,0.001325
13/09/2018,159.157,0.010206,0.00196
14/09/2018,158.363,-0.004989,0.00182
17/09/2018,158.968,0.00382,0.001963

有问题的代码行

for x in range(1,len(df.index)):
    if df["delta"].iloc[x] > 0:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1] * 13) + df["delta"].iloc[x]) / 14
    else:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1] * 13) + 0) / 14   

解决方法

我尝试使用.copy(),但仍然收到相同的错误信息。

for x in range(1,len(df.index)):
    if df["delta"].iloc[x] > 0:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1].copy() * 13) + df["delta"].iloc[x].copy()) / 14
    else:
        df["avg_gain"].iloc[x] = ((df["avg_gain"].iloc[x - 1].copy() * 13) + 0) / 14   

谢谢


df是由其他df的子集创建的,当你对它进行切片时,需要添加.copy()。 - BENY
我尝试添加了 .copy 但是没有成功。 - pythonlearner13
因为您分配了 df['avg_gain'].iloc[x],请参见此部分为什么使用链式索引时会导致分配失败? - Quang Hoang
我已经仔细查看了,但我无法找到这个特定情况的解决方案。 - pythonlearner13
我不确定你在这里尝试做什么,但我觉得使用df.shift()df.where()可能会有所帮助。 - Kevin Glasson
不幸的是,该方法只返回NaN值。 - pythonlearner13
1个回答

1
问题代码可以被替换为。
for x in range(1,len(df.index)):
    if df["delta"].iloc[x] > 0:
        df.iloc[x, -1] = ((df["avg_gain"].iloc[x - 1] * 13) + df["delta"].iloc[x]) / 14
    else:
        df.iloc[x,-1] = ((df["avg_gain"].iloc[x - 1].copy() * 13) + 0) / 14   

这是因为您最后添加了 avg_gain,所以您可以使用 iloc[:,-1] 访问该列。
使用 ewm 进行更新:
arg = df["delta"].clip(lower=0)
arg.iloc[0] = 0

df['avg_gain'] = arg.ewm(alpha=1/14, adjust=False).mean()

输出:

0     0.000000
1     0.000000
2     0.000000
3     0.000482
4     0.000997
5     0.001116
6     0.001251
7     0.001325
8     0.001960
9     0.001820
10    0.001962
Name: delta, dtype: float64

谢谢您的回答。然而,当我使用这段代码时,输出只是NaN。 - pythonlearner13
@pythonlearner13 看看我的修改。这只是一个快速而粗糙的修复方法。应该有一种更好的向量化方式。 - Quang Hoang
你能帮我解决这个问题吗?它跟这个链接有关:https://stackoverflow.com/questions/57870648/how-to-create-a-column-using-a-function-based-of-previous-values-in-the-column-i/57870840?noredirect=1#comment102167091_57870840 - pythonlearner13
难道不是同一个问题吗?将等号左边的 df["var"].iloc[x] 改为 df.iloc[x,-1],可以解决问题吗? - Quang Hoang
稍微有些不同。我试图摆脱循环,因为它很慢,而 df.iloc[x, -1] 仍然需要循环。 - pythonlearner13
这太有帮助了。我已经尝试很长时间来弄清楚这个问题。非常感谢。 - pythonlearner13

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接