【问题标题】:.loc[] = value returns SettingWithCopyWarning in Pandas.loc[] = value 在 Pandas 中返回 SettingWithCopyWarning
【发布时间】:2020-01-19 12:28:54
【问题描述】:

问题

我的代码收到以下错误消息。据说,问题是我首先用 .loc 对数据帧进行切片,然后尝试为该切片分配值。据我了解,Pandas 不能 100% 确定我是想为切片分配值,还是让它一直传播回原始 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()
  • 我尝试添加 .copy 但我不高兴
  • 因为你赋值df['avg_gain'].iloc[x],看这部分【为什么使用链式索引时赋值失败?】(pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html].
  • 我经历过,但我找不到这个特殊情况的解决方案
  • 我不确定你到底想在这里做什么,但我觉得这可以从使用 df.shift()df.where() 中受益

标签: python pandas loops


【解决方案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 查看我的编辑。这是一个快速的脏修复。应该有更好的矢量化方式。
  • 你有没有机会在这方面帮助我? stackoverflow.com/questions/57870648/…
  • 不是同一个问题吗?把=左边的df["var"].iloc[x]改成df.iloc[x,-1]?
  • 略有不同。我试图摆脱循环,因为它很慢而且 df.iloc[x, -1] 仍然意味着我需要一个循环
猜你喜欢
  • 2021-11-30
  • 2014-10-18
  • 1970-01-01
  • 2020-03-28
  • 2016-04-09
  • 1970-01-01
  • 2018-02-17
相关资源
最近更新 更多