Search code examples
python-3.xpandasdata-visualizationbokeh

Filter on datetime in Bokeh plot


I can get filtering on a datetime column working in Bokeh.

The following code/plot runs fine:

# imports
import pandas as pd
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, CDSView, GroupFilter

# dataframe and columndatasource
df = pd.DataFrame({'Date': list(pd.date_range(start='1/1/2018', end='1/03/2018')) * 3,
                   'Value': list(range(1, 10))})

source = ColumnDataSource(df)

Plot:

p = figure()
p.line(x='Date', y='Value', source=source)
show(p)

However, I only want to plot the rows with date 1/3/2018 without creating a new ColumnDataSource. Therfore I use GroupFilter and CDSView.

last_date = source.data['Date'].max() # select 1/3/2018
date_filter = GroupFilter(column_name='Date', group=str(last_date)) # create filter
view = CDSView(source=source, filters=[date_filter]) # create view

p = figure()
p.line(x='Date', y='Value', source=source, view=view) # use view
show(p)

But this plot shows no data? Any suggestions how to filter on the Date column?


Solution

  • You're hitting this issue: https://github.com/bokeh/bokeh/issues/7524 Right now, GroupFilter only works on strings.

    A workaround is to create any other kind of filter and filter the values yourself.