Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh- datetime x_range: 'ValueError, Unrecognized range input'

I have a dataframe df2 that looks like:

DATE       | STATUS
2018-02-01     A
2018-02-02     A
2018-02-02     B
..
2018-02-05     B

All the values are string type. I cannot seem to get the dates recognized as dates and plotted onto the x-axis of a stacked bar chart. Here is the most recent of my futile attempts:

import pandas as pd
from bokeh.io import curdoc
import numpy as np
from bokeh.models import ColumnDataSource, HoverTool,DatetimeTickFormatter
from bokeh.core.properties import value
from bokeh.plotting import figure

#prepare x axis
df2['DATE']=pd.to_datetime(df2['DATE'])
Months=df2["DATE"].tolist()
Months=list(set(Months))  #remove duplicates

IBs=df2["STATUS"].tolist()
IBs=list(set(IBs))

#pivot dataframe
#df2["DATE"]=df2["DATE"].str.strip() #trim values in column
df2=df2.pivot_table(index=['DATE'],columns=['STATUS'], aggfunc=len)
df2=df2.reset_index()
df2=df2.fillna(0) #replace nans with 0

#add plot
source=ColumnDataSource(data=df2)
p= figure(x_range= df2["DATE"], plot_width=700, plot_height=400, x_axis_type='datetime',
x_axis_label='Month', y_axis_label='count')
p.xaxis[0].formatter = DatetimeTickFormatter(days="%Y/%m/%d")

p.vbar_stack(IBs, x='DATE', width=0.9, source=source, legend=[value(x) for x in IBs])

curdoc().add_root(p)

thanks in advance.

edit: the errors are Urecognized range input: [Timestamp('2018-02-02 00:00:00').., Timestamp('2018-02-03 00:00:00')]

like image 360
machump Avatar asked Feb 08 '18 17:02

machump


1 Answers

I had the same issue and just figured out what was the problem (at least in my case).

Basically, whatever object you pass into x_range, it must be iterable, containing string type elements only.

Therefore, convert df2["DATE"]'s elements' type to str before you pass into x_range.

p = figure(x_range=df2["DATE"], ...)  # df2["DATE"] = list(map(str, df['score']))
like image 121
jha Avatar answered Nov 11 '22 23:11

jha