我似乎在 Bokeh 回调中遇到了 Pandas 代码的问题。
这是错误发生前的部分输出。我的数据框看起来很正常,但我不确定它为什么不正常
time temperature
0 2016-03-17 11:00:00 4.676
1 2016-03-17 11:30:00 4.633
2 2016-03-17 12:00:00 4.639
3 2016-03-17 12:30:00 4.603
4 2016-03-17 13:00:00 4.615
5 2016-03-17 13:30:00 4.650
6 2016-03-17 14:00:00 4.678
7 2016-03-17 14:30:00 4.698
8 2016-03-17 15:00:00 4.753
9 2016-03-17 15:30:00 4.847
ERROR:bokeh.server.protocol_handler:error handling message Message 'PATCH-DOC' (
revision 1): ValueError('window must be an integer',)
这是我从 flask 嵌入示例 (link here) 更改的代码:
def callback(attr, old, new):
df = pd.DataFrame.from_dict(source.data.copy())
print df[:10]
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new)).mean()
source.data = ColumnDataSource(data=data).data
slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
slider.on_change('value', callback)
如果有帮助,我也可以包括完整的代码,但我所做的主要更改只是一个定期获取新数据的 doc.add_periodic_callback()。
最佳答案
截至今天,the documentation状态如下:
window : int, or offset
Size of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size.
If its an offset then this will be the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes. This is new in 0.19.0
我不清楚您的数据框中的时间信息是列还是 MultiIndex 的一部分。对于第一种情况,您可以使用.set_index('time')
。
对于 MultiIndex,目前,您不能使用偏移量。 See the related issue .相反,您可以使用 .reset_index()
将其转换为单个索引数据帧(参见 here )。
更新:您还可以使用 on
参数为基于偏移量的滚动指标传递日期时间列(因此,您不必将它们包含在索引中)。
关于 python , Pandas ;值错误 ('window must be an integer' ,),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569205/