The Bokeh DateRangeSlider
widget requires int
values for its step
attribute which must be the time value in milliseconds. It works well when the step is set to seconds
, minutes
, hours
, days
or years
. However I need a month resolution on the slider.
When the step is set to 31 days it works well for the start date until March when instead of 1 March
I get 4 March
. Then the shift from 1th of the month in the displayed value gets bigger and bigger.
I want to be able to set and get displayed the slider range on both sides always to be the 1th day of the month e.g. 1 March
, 1 April
, 1 May
, 1 June
etc... like it is in the DataFrame
.
Considering the following code, what would be the best way to realize it (possibly using a JS callback) ?
import pandas as pd
from bokeh.plotting import show
from bokeh.models import DateRangeSlider
data = {'date_start': ['201812', '201901', '201902', '201903', '201904', '201905', '201906', '201907', '201908', '201909', '201910', '201911'],
'date_end': [ '201901', '201902', '201903', '201904', '201905', '201906', '201907', '201908', '201909', '201910', '201911', '201912'],
'values' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
df = pd.DataFrame(data)
df['Start'] = pd.to_datetime(df['date_start'], format='%Y%m')
df['End'] = pd.to_datetime(df['date_end'], format='%Y%m')
start_date = df['Start'].min()
end_date = df['End'].max()
range_slider = DateRangeSlider(start=start_date, end=end_date, value=(start_date, end_date), step=31*24*60*60*1000, title="Date Range", callback_policy = 'mouseup', tooltips = False, width=600)
show(range_slider)import pandas as pd
from bokeh.plotting import show
from bokeh.models import DateRangeSlider
data = {'date_start': ['201812', '201901', '201902', '201903', '201904', '201905', '201906', '201907', '201908', '201909', '201910', '201911'],
'date_end': [ '201901', '201902', '201903', '201904', '201905', '201906', '201907', '201908', '201909', '201910', '201911', '201912'],
'values' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
df = pd.DataFrame(data)
df['Start'] = pd.to_datetime(df['date_start'], format='%Y%m')
df['End'] = pd.to_datetime(df['date_end'], format='%Y%m')
start_date = df['Start'].min()
end_date = df['End'].max()
range_slider = DateRangeSlider(start=start_date, end=end_date, value=(start_date, end_date), step=31*24*60*60*1000, title="Date Range", callback_policy = 'mouseup', tooltips = False, width=600)
show(range_slider)