为什么bokeh app回调会转换变量类型? (ColumnDataSource 到 pandas df)

Why does the bokeh app callback convert variable type? (ColumnDataSource to pandas df)

我在 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

其中时间为 datetime64[ns],Y 为 float64,标签来自对象类型。

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

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() 或匹配索引来重建时间戳,但我如何更改回调以将原始条目保留在时间中?

how can I change the callback to keep the original entries in Time?

你不能。日期时间值的实际底层传输格式是 millseconds since epoch,这就是 Bokeh 在浏览器中序列化、发送到 BokehJS 或与 BokehJS 同步时自动将任何和所有日期时间类型转换成的格式.在独立(非服务器)情况下,这从来都不是问题,因为数据永远不会 "comes back" 到 Python 进程。但它在 Bokeh 服务器上下文中可能会令人惊讶。您需要将时间戳值转换回您想要的任何日期时间类型(有很多),或者如果您只是想确保原始值不受干扰,请事先制作一个副本。