Bokeh converts my datetime columns to int, but it is unclear when

My problem is quite hard to explain so I’ll try to be as clear as possible.
I’m using Bokeh to run a webapp on a server. The main functionality is showing a datatable and let the user interact with it.
My data_table comes from a pandas dataframe, where there are some dates stored as datetime64.
Earlier, I incorporated filters for the datatable using the daterangerslider widgets.
The slider is using date in a ‘ms since epoch’ format. I noticed through testing that when building my datatable, bokeh seems to convert my datetime64 to the same format. I was then able to compare dates using this format, and everything worked as expected.
The problem came when I tested this after updating the datatable. In my full use case, the table is sometimes updated, which i do by changing the source.data. However, when I do that, the datetimes stay in datetime format instead of being converted to int, the same way they do when I create the table.

I’ve probably lost you right now, so here is a bare minimum example to understand my struggle.
In this bit of code, i have :

  • a very simple dataframe that defines a datatable, with one column containing dates
  • two buttons : the first one sends the values of source.data to log so i can check it, the second one, just redefines source.data the same way I do when creating the datatable.

Here is the code :

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, Button
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import column, row, Spacer

import logging
import bokeh
import numpy as np
import pandas as pd

def to_log():
    logging.warning(source.data['date'])

def update_source():
    source.data=df 

df = pd.DataFrame({'test':['foo', 'bar'],
                         'date':[np.datetime64('now'), np.datetime64('now')]
                          })
    
source = ColumnDataSource(df)
source.data=df

columns = [TableColumn(field='test'),
          TableColumn(field='date')]

data_table = DataTable(source=source, columns=columns)

to_log_button = Button(label="send to log", button_type="success", width = 150)
to_log_button.on_click(to_log)

update_source_button = Button(label="update source", button_type="success", width = 150)
update_source_button.on_click(update_source)

lay = column(to_log_button, update_source_button, data_table)

curdoc().add_root(lay)

When running this, if I click the “to log” button, I get this :

2021-12-27 18:57:12,400 WARNING [1.64062783e+12 1.64062783e+12]

Which is the first part of my message : bokeh seems to convert my dates to int (ms since epoch if I get it right).
However, if I press the “update source” button, which simply redefines the source the same as before, and then send the values to log, i get this :

2021-12-27 18:57:13,813 WARNING ['2021-12-27T17:57:09.000000000' '2021-12-27T17:57:09.000000000']

This time, my datetime are kept as datetime.
Don’t mind the redundancy of these two lines

source = ColumnDataSource(df)
source.data=df

I was just so out of ideas that I wanted to check if the problem came from a difference between doing source=ColumnDataSource(df) vs source.data=df

Anyone knows what’s going on ? Is it intended, and if so, what is it that I’m not getting ?

Thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.