【问题标题】:Python Warning:" SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame" [duplicate]Python 警告:“SettingWithCopyWarning:试图在来自 DataFrame 的切片的副本上设置一个值”[重复]
【发布时间】:2020-10-16 10:00:33
【问题描述】:

我创建了这个新的数据框来存储这 2 列:

#Create a new data frame
new_df = df[period-1:]

new_df['Buy'] = get_signal(new_df)[0]
new_df['Sell'] = get_signal(new_df)[1]

但是给了我这个错误:

[Command: python -u C:\Users\Nicolò\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py]
C:\Users\Nicol�\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py:80: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  new_df['Buy'] = get_signal(new_df)[0]
C:\Users\Nicol�\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py:81: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  new_df['Sell'] = get_signal(new_df)[1]
Traceback (most recent call last):
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\cbook\__init__.py", line 196, in process
    func(*args, **kwargs)
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 951, in _start
    self._init_draw()
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1743, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\animation.py", line 1766, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Users\Nicol�\Documents\Git\ProgettoTradingBot\ProgettoTradeBot\GUIprova2.py", line 98, in animate
    a.fill_between(x_axis,new_df['Upper'],new_df['Lower'], color = 'grey', alpha= 0.5)
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\__init__.py", line 1565, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\Nicol�\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\axes\_axes.py", line 5158, in fill_between
    where = where & ~functools.reduce(np.logical_or,
ValueError: operands could not be broadcast together with shapes (2208,) (2189,) 
[Finished in 79.149s]

这是完整的文件:

https://github.com/Raccispini/ProgettoTradeBot/blob/master/GUIprova2.py

如何将get_signal(new_df) 数组复制到new_df['Buy']new_df['Sell'] 而不收到此警告?

谢谢:)

编辑: 我之前在另一个项目中完成了这个程序并且它有效。我不明白为什么这里没有工作。 我照原样复制粘贴。

【问题讨论】:

  • 您是否尝试过按照建议使用.loc[row_indexer,col_indexer] new_df.loc[:, 'Buy'] = get_signal(new_df)[0]
  • 完成但给了我:ValueError: cannot set a row with mismatched columns

标签: python pandas dataframe tkinter


【解决方案1】:

您已创建 new_df 调用:

new_df = df[period-1:]

所以 new_df 实际上是原始 DataFrame 的视图

然后您尝试修改它,调用 new_df['Buy'] = ...new_df['Sell'] = ...,所以会出现这个警告。

也许您应该将 new_df 创建为“独立”数据框,并使用 它自己的数据缓冲区:

new_df = df[period-1:].copy()

那么你就可以随意修改它的内容了。

【讨论】:

    猜你喜欢
    • 2016-12-22
    • 2016-02-17
    • 2021-03-05
    • 2016-06-16
    • 2019-03-30
    • 2018-09-18
    • 2021-08-09
    • 2021-07-26
    相关资源
    最近更新 更多