Python,熊猫; ValueError('window必须是整数',)

时间:2018-05-28 15:04:25

标签: python python-2.7 flask

我似乎在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()。

3 个答案:

答案 0 :(得分:3)

截至今天,the documentation的陈述如下:

  

窗口:整数或偏移量

     

移动窗口的大小。这是用于   计算统计信息。每个窗口的大小都是固定的。

     

如果它是一个偏移量,那么这将是每个窗口的时间段。每   窗口将根据其中包含的观察结果而变化   时间段。这仅对类似datetime的索引有效。这是   0.19.0中的新功能

我不清楚时间信息是数据框中的列还是MultiIndex的一部分。对于第一种情况,您可以使用.set_index('time')

对于MultiIndex,当前不能使用偏移量。 See the related issue。如果可行,则可以使用.reset_index()将其转换为单个索引数据帧(请参见here)。

更新:您还可以使用on参数为基于偏移量的滚动指标传递datetime列(因此,您不必具有索引)。

答案 1 :(得分:0)

这是Pandas的错误。您正在将字符串传递给df.rolling,但它需要only integer values。您可能想要传递int(new)

编辑:如下所示,显然Pandas文档是不完整的,在这种情况下真正的最终问题可能是缺少时间索引,因为创建一个天真的Dataframe并传递像"10d"这样的值肯定会提高指示错误:

In [2]: df = pd.DataFrame({'B': [0, 1, 2, 10, 4]})

In [3]: df.rolling('10d')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-2a9875316cd7> in <module>
----> 1 df.rolling('10d')

~/anaconda/lib/python3.7/site-packages/pandas/core/generic.py in rolling(self, window, min_periods, center, win_type, on, axis, closed)
   8906                                    min_periods=min_periods,
   8907                                    center=center, win_type=win_type,
-> 8908                                    on=on, axis=axis, closed=closed)
   8909
   8910         cls.rolling = rolling

~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in rolling(obj, win_type, **kwds)
   2467         return Window(obj, win_type=win_type, **kwds)
   2468
-> 2469     return Rolling(obj, **kwds)
   2470
   2471

~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in __init__(self, obj, window, min_periods, center, win_type, axis, on, closed, **kwargs)
     78         self.win_freq = None
     79         self.axis = obj._get_axis_number(axis) if axis is not None else None
---> 80         self.validate()
     81
     82     @property

~/anaconda/lib/python3.7/site-packages/pandas/core/window.py in validate(self)
   1476
   1477         elif not is_integer(self.window):
-> 1478             raise ValueError("window must be an integer")
   1479         elif self.window < 0:
   1480             raise ValueError("window must be non-negative")

ValueError: window must be an integer

答案 2 :(得分:0)

df.rolling也可以处理时间段。确保日期时间为pandas格式。如果不是,则进行转换-

data['col'] = pd.to_datetime(data['col'])