machump
machump

Reputation: 1267

Update Bokeh ranges with Pandas Period objects

Say I have a dataframe line_src_yr like:

Date Value
2017 100
2018 200

and a second dataframe dfQtr like:

Date   Value
2017Q1  25
2017Q2  25
2017Q3  25
2017Q4  25
2018Q1  100
2018Q2  100

I'd like to be able to alternate between the dataframes underlying my plot using a radio button:

QtrLineSrc=ColumnDataSource(data=dfQtr)
line_src_yr=ColumnDataSource(data=line_df_yr)

if radio_group.active==0:
    p_line.xaxis_type='datetime'             
    p_line.xaxis.formatter=DatetimeTickFormatter(days=["%Y"])
    line_src_yr.data={"Date":line_df_yr["Date"],"Value":line_df_yr['Value']}
elif radio_group.active==1:
    line_src_yr.data=QtrLineSrc.data
    p_line.x_range=list(set(dfQtr["Date"]))

radio_group=RadioGroup(labels=["Annual","Quarter"], active =0)
radio_group.on_change('active', update)

p_line=figure(plot_width= 750, plot_height=450)
p_line.line(x="Date",y="Value", source=line_src_yr)

But I get

:Error running application handler : Unrecognized range input: '[Period('2017Q3', 'Q-DEC'), Period('2018Q1', 'Q-DEC'), Period('2018Q2', 'Q-DEC'), Period('2017Q4', 'Q-DEC'), Period('2017Q1', 'Q-DEC'), Period('2018Q3', 'Q-DEC'), Period('2018Q4', 'Q-DEC'), Period('2017Q2', 'Q-DEC')]'

I suspect its due to dfQtr not being datetime, but if I try to convert to such, it yields <class 'pandas._libs.tslibs.period.Period'> is not convertible to datetime

Upvotes: 0

Views: 111

Answers (1)

bigreddot
bigreddot

Reputation: 34568

Support for Pandas Period was only added very recently, in Pull Request #8027. As of today that is newer than any full release of Bokeh. The work will be part of the next release, in late October 2018.

However, looking more closely, it also appears you are trying to set the range from a list?

p_line.x_range = list(set(dfQtr["Date"])) # not valid

This is not ever valid usage. It is similar to usage for categorical ranges. If your plot has a categorical range, you'd want something more like:

p_line.x_range.factors = [str(x) for x in set(dfQtr["Date"])]

Note that categorical values can only be strings. If you are using a categorical range you can do this already, by converting the Period objects to whatever string representation you want them to have. If you are not using a categorical range then it's not clear what you are trying to achieve.

Upvotes: 1

Related Questions