【问题标题】:Python, Pandas ; ValueError('window must be an integer',)蟒蛇,熊猫; ValueError('窗口必须是整数',)
【发布时间】:2018-11-07 05:09:13
【问题描述】:

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

这是我从烧瓶嵌入示例 (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() 定期获取新数据。

【问题讨论】:

    标签: python python-2.7 flask


    【解决方案1】:

    df.rolling 也可以处理时间段。确保日期时间为pandas 格式。如果没有,就这样转换 -

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

    【讨论】:

    • 确保转换为日期时间类型,然后在该列上运行“set_index”。这为我解决了问题。
    【解决方案2】:

    截至今天,the documentation 声明如下:

    窗口:整数或偏移量

    移动窗口的大小。这是用于观察的数量 计算统计量。每个窗口的大小都是固定的。

    如果它是一个偏移量,那么这将是每个窗口的时间段。每个 窗口将根据包含在 时间段。这仅对 datetimelike 索引有效。这是 0.19.0 中的新功能

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

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

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

    【讨论】:

    • .set_index('time') 修复了我的数据框,然后用 mean() 解决了问题
    【解决方案3】:

    这是来自 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
    

    【讨论】:

    • 感谢您的回复,我仍然不确定为什么 pandas 不高兴,因为我没有更改源代码的任何内容,但我会尝试弄清楚。我没有意识到散景只是传递错误消息
    • 答案是错误的。 df.rolling 也可以处理像'10D' 这样的时间段(如果new10,这里就是这种情况)。所以这个错误一定有不同的原因。
    • 可能问题是新获取的数据没有时间索引?
    • 也许你应该做一个有用的 PR 来更新 Pandas 文档,因为它目前明确指出 window 需要一个整数。
    • 如果索引不是 pandas 时间索引,就会发生这种情况。见@madhurs 回答
    猜你喜欢
    • 2021-11-09
    • 2018-08-20
    • 2016-10-16
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    相关资源
    最近更新 更多