Python警告:“ SettingWithCopyWarning:试图在来自DataFrame的切片的副本上设置一个值”

时间:2020-10-16 10:00:33

标签: python pandas dataframe tkinter

我创建了这个新的数据框来存储这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']中?

谢谢:)

编辑: 我之前已经在另一个项目中完成了该程序,并且可以工作。我不明白为什么这里要工作。 我复制的粘贴与以前完全一样。

1 个答案:

答案 0 :(得分:3)

您已创建 new_df 调用:

new_df = df[period-1:]

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

然后您尝试修改它,调用new_df['Buy'] = ...new_df['Sell'] = ...,因此会发生此警告。

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

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

然后您可以自由修改其内容。