datetime.date values in a ColumnDataSource column serialize to ISO strings instead of numeric timestamps, silently breaking glyph positioning
Problem description
.
Feature description
Software versions
Python 3.12.11
Bokeh 3.8.1Description
When a ColumnDataSource column holds plain datetime.date objects (not datetime.datetime), Serializer._encode_other (in bokeh/core/serialization.py) falls through to obj.isoformat(), producing an array of date strings:
if is_datetime_type(obj):
return convert_datetime_type(obj)
if is_timedelta_type(obj):
return convert_timedelta_type(obj)
if isinstance(obj, dt.date):
return obj.isoformat() # <-- bare `date` lands hereis_datetime_type (in bokeh/util/serialization.py) only recognizes datetime.datetime/datetime.time/numpy.datetime64 — datetime.date isn't included, so it never reaches the numeric (convert_datetime_type) branch and instead gets the string treatment intended for Date-typed properties (e.g. annotation fields), not for arbitrary glyph coordinate data.
The result: a glyph renderer whose x (or y) field is a list of datetime.date values gets a string array sent to BokehJS instead of millisecond-epoch numbers. Since the figure's scale (LinearScale, used for both "linear" and "datetime" axis types — DatetimeAxis only changes tick formatting) expects numeric input, the glyph silently fails to position any points. No exception, no BokehUserWarning — the chart just renders empty.
The equivalent datetime.datetime value (same calendar day, time component present) serializes correctly to a numeric timestamp, so this is specifically about the date-only type.
Reproduction
Pure Bokeh, no other dependencies:
from datetime import date, datetime
from bokeh.core.serialization import Serializer
from bokeh.plotting import figure
dates = [date(2024, 1, 1), date(2024, 1, 2), date(2024, 1, 3)]
values = [1, 2, 3]
p = figure(x_axis_type="datetime")
r = p.line(x=dates, y=values)
print(Serializer().encode(r.data_source.data["x"]))
# ['2024-01-01', '2024-01-02', '2024-01-03'] <- strings, chart renders blank
# Same dates, but as datetime instead of date:
dt_dates = [datetime(2024, 1, 1), datetime(2024, 1, 2), datetime(2024, 1, 3)]
r2 = figure(x_axis_type="datetime").line(x=dt_dates, y=values)
print(Serializer().encode(r2.data_source.data["x"]))
# [1704067200000.0, 1704153600000.0, 1704240000000.0] <- numeric, works correctlyThis is a realistic case to hit by accident, not just a contrived one — both pandas (Series.dt.date, producing an object-dtype column of datetime.date) and Polars (Date dtype, as opposed to Datetime) commonly produce columns of bare datetime.date values, and converting a DataFrame to a ColumnDataSource via to_dict() preserves that type as-is.
Expected behavior
Either:
is_datetime_type/_encode_othertreats baredatetime.datethe same asdatetime.datetime(e.g. viaconvert_date_to_datetime, which already exists and is used by theDatetimeproperty class's.transform()) when encoding genericColumnDataSourcecolumn data, so it serializes numerically — or- If treating
dateas a string is intentional for back-compat with whatever currently relies onDate-typed properties accepting ISO strings, thenColumnDataSource/glyph data which contains a column ofdatetime.datevalues used as a coordinate (x/y/etc.) should raise aBokehUserWarningso the empty-chart failure isn't silent.
Happy to put together a PR for whichever direction maintainers prefer — wanted to raise it first since it touches the serializer's general dispatch logic and I wasn't sure which behavior is intended.
Potential alternatives
.
Additional information
No response
Source: bokeh/bokeh