Uh oh!
There was an error while loading. Please reload this page.
Fix add_vline and add_hline with datetime axes - #5508
Conversation
emilykl
commented
Feb 19, 2026
Thanks for the contribution @mosh3eb ! This approach won't work because the |
Instead of assuming input values are equal, _mean() now converts datetime-like values (date strings and datetime objects) to milliseconds-since-epoch, computes the actual arithmetic mean, and converts back. This correctly handles annotation placement for ALL shapes (vlines, hlines, vrects, hrects) with datetime axes. The numeric fast path is unchanged — datetime handling only activates when sum() raises TypeError on non-numeric types. Added tests for datetime strings, datetime objects, and rects with different x0/x1 values on datetime axes. Fixesplotly#3065
c580b1f to
9bed9d3CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
When using add_vline, add_hline,
add_vrect, oradd_hrectwithannotation_texton datetime axes, plotly crashes with aTypeErrorbecause the _mean() helper function tries to callfloat(sum(x))on datetime strings.Approach
Instead of assuming input values are equal (which only works for vlines/hlines), _mean() now converts datetime-like values to milliseconds-since-epoch, computes the actual arithmetic mean, and converts back. This correctly handles annotation placement for ALL shapes with datetime axes, including rects where
x0 != x1.sum()raisesTypeError"2018-09-24") anddatetime.datetimeobjectsTypeErrorfor truly unsupported typesChanges
Fixes#3065