1

我在 jupyter 中使用嵌入式散景应用程序来标记时间序列的各个部分。假设我们必须遵循示例数据框:

    Time                Y           Label
0   2018-02-13 13:14:05 0.401028    a
1   2018-02-13 13:30:46 0.900101    a
2   2018-02-13 13:40:06 -0.648143   a
3   2018-02-14 16:33:27 1.111675    a
4   2018-03-13 11:43:16 -0.986025   a

其中 Time 是 datetime64[ns],Y 是 float64,Label 来自类型对象。

现在,我使用以下散景应用程序通过用户输入更改标签的条目,并通过单击按钮触发回调。

def modify_doc(doc):
    p = figure(tools=["pan, box_zoom, wheel_zoom, reset, save, xbox_select"])
    source=ColumnDataSource(df_test)
    p.line(x="index", y="Y", source=source)
    p.circle(x="index", y="Y", source=source, alpha=0)
    def callback():
        global list_new
        list_new = []
        inds = source.selected.indices
        for j in inds:
            source.data["Label"][j] = label_input.value.strip()
        list_new.append(pd.DataFrame(source.data))
    label_input = TextInput(title="Label")
    button = Button(label="Label Data")
    button.on_click(callback)
    layout = column(p, label_input, button)
    doc.add_root(layout)
show(modify_doc)

在此处输入图像描述

不要怀疑 list_new,这是一种必要的方法,因为我使用了多个时间序列图和 ColumnDataSource 对象。

回调后,我得到了接受的标签输出:

    Label   Time        Y           index
0   a   1.518528e+12    0.401028    0
1   a   1.518529e+12    0.900101    1
2   b   1.518529e+12    -0.648143   2
3   b   1.518626e+12    1.111675    3
4   b   1.520941e+12    -0.986025   4

但是为什么时间会被转换为浮点数?我知道如何通过使用 datetime.datetime.utcfromtimestamp() 或匹配索引来重建时间戳,但是如何更改回调以将原始条目保留在时间中?

4

1 回答 1

1

如何更改回调以将原始条目保留在时间中?

你不能。datetime 值的实际基础线格式是自 epoch 以来的毫秒数,这就是 Bokeh 在浏览器中序列化、发送到 BokehJS 或与 BokehJS 同步时会自动将任何和所有 datetime 类型转换为的格式。在独立(非服务器)情况下,这不是问题,因为数据永远不会“返回”到 Python 进程。但在 Bokeh 服务器环境中可能会令人惊讶。您需要将时间戳值转换回您想要的任何日期时间类型(有很多),或者如果您只是想确保原始值不受干扰,请事先制作一份副本。

于 2019-10-14T14:39:52.027 回答