2016-01-24 59 views
7

我試圖使用散景來繪製Pandas數據框,其中DateTime列包含年份和數字。如果DateTime被指定爲x,則行爲是預期的(在x軸上的年數)。但是,如果我使用​​將DateTime列轉換爲數據幀的索引,然後只在TimeSeries中指定y,則我會在x軸上獲得以毫秒爲單位的時間。一個最小的例子使用帶索引的數據框的散景TimeSeries

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) 

這是預期的行爲或錯誤?我期望兩種方法都有相同的畫面。

乾杯!

+0

這在我看來不一致了。有趣的是,在'fig2 = TimeSeries(test,y ='foo')'行之後,'test'已被改變爲包括帶有'datetime'數據的索引和一個名爲'index'的新列。簡單地繪製一個數據框會改變數據有點令人驚訝。 – Jake

+0

好,我沒有注意到。我剛剛報告這是[問題](https://github.com/bokeh/bokeh/issues/3763)。 – manu

回答

0

散景用於添加索引的內部原因,但不是最近版本(> = 0.12.x)它不再這樣做。值得注意的是bokeh.charts API已被棄用和刪除。使用穩定bokeh.plotting API等效代碼產生預期的結果:

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

enter image description here