【发布时间】: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