Skip to content

Serialize datetime-like values in _orjson_default (fixes #458)#461

Open
gaoflow wants to merge 1 commit into
plotly:masterfrom
gaoflow:fix-458-datetime-serialization
Open

Serialize datetime-like values in _orjson_default (fixes #458)#461
gaoflow wants to merge 1 commit into
plotly:masterfrom
gaoflow:fix-458-datetime-serialization

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Exporting a figure that contains a datetime-like value — e.g. a pandas Timestamp used as a marker position — raises:

TypeError: Type is not JSON serializable: Timestamp

(reported in #458, a regression in 1.3.0). The orjson fallback _orjson_default only handles Decimal and objects exposing .tolist() (NumPy). A Timestamp has neither, so it falls through to raise TypeError.

Fix

Serialize datetime-like objects — anything with .isoformat() (pandas Timestamp, datetime, date) — to ISO strings, which is how Plotly's own JSON encoder represents them. The check is placed before the .tolist() branch so numpy.datetime64 (which has .tolist() but no .isoformat()) is unaffected.

Tests

Added a unit test for _orjson_default covering a Timestamp, a date, an end-to-end orjson.dumps of a spec carrying a Timestamp, and the existing Decimal/NumPy fallbacks. It fails on master with the original TypeError and passes with the fix.

A figure containing a pandas Timestamp (e.g. a marker x-position) raised
`TypeError: Type is not JSON serializable: Timestamp` from write_image /
calc_fig. The orjson fallback `_orjson_default` only handled `Decimal`
and objects exposing `.tolist()` (NumPy); a Timestamp has neither, so it
fell through to `raise TypeError`.

Serialize datetime-like objects (anything with `.isoformat()`, e.g.
pandas `Timestamp`, `datetime`, `date`) to ISO strings, matching how
Plotly's own JSON encoder handles them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant