使用带索引的数据框在Bokeh中进行TimeSeries

man*_*anu 9 python time-series pandas bokeh

我正在尝试使用Bokeh绘制一个包含年份和数字列的Pandas数据框DateTime.如果DateTime指定为x,则行为是预期的(x轴上的年份).但是,如果我用set_index打开DateTime列入数据帧的指数,那么只有指定yTimeSeries在x轴毫秒我得到的时间.一个最小的例子

import pandas as pd
import numpy as np
from bokeh.charts import TimeSeries, output_file, show

output_file('fig.html')
test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})
fig = TimeSeries(test,x='datetime',y='foo')
show(fig)

output_file('fig2.html')
test = test.set_index('datetime')
fig2 = TimeSeries(test,y='foo')
show(fig2)
Run Code Online (Sandbox Code Playgroud)

这是预期的行为还是错误?我希望两种方法都有相同的图片.

干杯!!

big*_*dot 1

Bokeh 过去常常出于内部原因添加索引,但从不太新的版本 (>= 0.12.x) 开始,它不再这样做。另外值得注意的是,该bokeh.chartsAPI 已被弃用并删除。使用稳定 API 的等效代码bokeh.plotting可产生预期结果:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row

output_file('fig.html')

test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})

fig = figure(x_axis_type="datetime")
fig.line(x='datetime',y='foo', source=test)

test = test.set_index('datetime')

fig2 = figure(x_axis_type="datetime")
fig2.line(x='datetime', y='foo', source=test)
show(row(fig, fig2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述