Python, pandas ;ValueError('窗口必须为整数',) [英] Python, Pandas ; ValueError('window must be an integer',)

查看:94
本文介绍了Python, pandas ;ValueError('窗口必须为整数',)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在Bokeh回调中遇到了熊猫代码的问题.

I seem to be having this issue with Pandas code inside a Bokeh callback.

这是错误之前输出的一部分.我的数据框看起来很正常,我不确定为什么会感到沮丧

Here's part of the output before the error. My dataframe seems normal and I'm not sure why it's upset

                     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',)

这是我从烧瓶嵌入示例中更改的代码(链接此处):

And here's the code I changed from the flask embed example (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(),它会定期获取新数据.

I can also include the full code if that help, but the main change I have is just a doc.add_periodic_callback() that fetches new data periodically.

推荐答案

这是Pandas的错误.您正在将字符串传递给 df.rolling ,但是它期望仅整数值.您可能想要传递 int(new).

This is an error from Pandas. You are passing a string to df.rolling, but it expects only integer values. You probably want to pass int(new) instead.

如下所述,熊猫文件显然是不完整的,在这种情况下,真正的最终问题可能是缺少时间索引,因为创建了朴素的Dataframe并传递了"10d"之类的值肯定会引起指示的错误:

as noted below, evidently the Pandas documentation is incomplete, and the real ultimate problem in this case is probably the lack of a time index, since creating a naive Dataframe and passing values like "10d" definitely raises the indicated error:

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

这篇关于Python, pandas ;ValueError('窗口必须为整数',)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆