Python 警告:" SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFram



我创建了这个新的数据帧来存储这两列:

#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:UsersNicolòDocumentsGitProgettoTradingBotProgettoTradeBotGUIprova2.py]
C:UsersNicol�DocumentsGitProgettoTradingBotProgettoTradeBotGUIprova2.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:UsersNicol�DocumentsGitProgettoTradingBotProgettoTradeBotGUIprova2.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:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlibcbook__init__.py", line 196, in process
func(*args, **kwargs)
File "C:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlibanimation.py", line 951, in _start
self._init_draw()
File "C:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlibanimation.py", line 1743, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "C:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlibanimation.py", line 1766, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "C:UsersNicol�DocumentsGitProgettoTradingBotProgettoTradeBotGUIprova2.py", line 98, in animate
a.fill_between(x_axis,new_df['Upper'],new_df['Lower'], color = 'grey', alpha= 0.5)
File "C:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlib__init__.py", line 1565, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:UsersNicol�AppDataLocalProgramsPythonPython38libsite-packagesmatplotlibaxes_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']

谢谢:(

编辑:我以前在另一个项目中做过这段程序,它很有效。我不明白为什么这里不起作用。我复制粘贴的和以前一模一样。

您创建了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()

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

相关内容

  • 没有找到相关文章

最新更新