【问题标题】:SettingWithCopyWarning & Hidden ChainingSettingWithCopyWarning 和隐藏链接
【发布时间】:2018-05-03 21:42:02
【问题描述】:

我收到的 SettingWithCopyWarning 提示我可能存在链接问题。

SettingWithCopyWarning: 试图在 DataFrame 中的切片副本上设置一个值。 尝试改用 .loc[row_indexer,col_indexer] = value

我已详细阅读过此内容,但似乎无法为我的用例找到正确的解决方案。这是一篇关于该主题的精彩文章:Understanding SettingwithCopyWarning in pandas

但是,我仍然不太确定如何进行。以下是产生最终结果的同一行代码的三个变体,但都抛出了相同的错误。

变化:

  1. X[subindex + '_DE'] = X[subindex + '_DE'].clip(lower=0, upper=200, axis=0)

  2. X.loc[:, subindex + '_DE'] = np.clip(df.loc[:, subindex + '_DE'], 0, 200)

  3. X.loc[:, subindex + '_DE'] = X.loc[:, subindex + '_DE'].clip(lower=0, upper=200, axis=0)

最终目标:只需剪裁(截断)列 [subindex + '_DE'] 中超出下限 (0) 和上限 (200) 的任何值。

我不确定如何继续。一点指导会很有帮助。提前谢谢你。

有用的背景信息:

X 是一个由 float64 数据组成的 pandas 数据框,按 20 列(特征)x 6514 行(观察)排列。

这里有一些数据可以使用:

> print(X[subindex + '_DE'].head(180))

> date 
> 1999-12-31    33.6584 
> 2000-01-01    33.6584 
> 2000-01-02    33.6584
> 2000-01-03    33.6584
> ... ...
> 2000-06-25    32.6530
> 2000-06-26    32.6530
> 2000-06-27    32.6530
> Name: NYEPLC_DE, Length: 180, dtype: float64

【问题讨论】:

  • 提供一些数据来处理:X.head(20)
  • X 是如何定义的?
  • 更新了原始帖子,其中包含两个(sharatpc 和 IanS)问题的答案。

标签: python pandas dataframe


【解决方案1】:

找到答案了。

def 中,我设置了X = df[](一个数据框)。如果我只是将.copy() 添加到df[],那么警告就会消失。

例如X = df[['column1', 'column2']].copy()

Benjamin Pryke 上面的文章非常好……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2011-05-17
    相关资源
    最近更新 更多