9

I'm trying to use Bokeh to plot a Pandas dataframe with a DateTime column containing years and a numeric one. If the DateTime is specified as x, the behaviour is the expected (years in the x-axis). However, if I use set_index to turn the DateTime column into the index of the dataframe and then only specify the y in the TimeSeries I get time in milliseconds in the x-axis. A minimal example

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)

Is this the expected behaviour or a bug? I would expect the same picture with both approaches.

Cheers!!

manu
  • 1,333
  • 2
  • 11
  • 24
  • This looks inconsistent to me, too. Interestingly, after the `fig2 = TimeSeries(test,y='foo')` line, `test` has been altered to include both an index with the `datetime` data and a new column called `index`. It's a bit surprising that simply plotting a data frame would alter the data. – Jake Jan 25 '16 at 08:31
  • Good catch, I had not noticed. I just reported this as [an issue](https://github.com/bokeh/bokeh/issues/3763). – manu Jan 27 '16 at 08:05

1 Answers1

1

Bokeh used to add an index for internal reasons but as of not-so-recent versions (>= 0.12.x) it no longer does this. Also it's worth noting that the bokeh.charts API has been deprecated and removed. The equivalent code using the stable bokeh.plotting API yields the expected result:

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

bigreddot
  • 33,642
  • 5
  • 69
  • 122