Skip to content

Fix add_vline and add_hline with datetime axes - #5508

Merged
emilykl merged 6 commits into
plotly:mainfrom
mosh3eb:fix/add-vline-datetime-annotation
May 8, 2026
Merged

Fix add_vline and add_hline with datetime axes#5508
emilykl merged 6 commits into
plotly:mainfrom
mosh3eb:fix/add-vline-datetime-annotation

Conversation

@mosh3eb

@mosh3ebmosh3eb commented Feb 14, 2026

Copy link
Copy Markdown
Contributor

Summary

When using add_vline, add_hline, add_vrect, or add_hrect with annotation_text on datetime axes, plotly crashes with a TypeError because the _mean() helper function tries to call float(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.

  • The numeric fast path is unchanged -- datetime handling only activates when sum() raises TypeError
  • Handles both date strings ("2018-09-24") and datetime.datetime objects
  • Re-raises TypeError for truly unsupported types

Changes

  • plotly/shapeannotation.py: Added _is_date_string(), _datetime_str_to_ms(), _ms_to_datetime_str() helpers and updated _mean() with try/except fallback
  • tests/test_optional/test_autoshapes/test_annotated_shapes.py: Added 5 tests covering datetime strings, datetime objects, vlines, hlines, and vrects

Fixes#3065

@emilykl

Copy link
Copy Markdown
Contributor

Thanks for the contribution @mosh3eb ! This approach won't work because the _mean() function is used to determine annotation placement for ALL lines, not just vertical/horizontal lines, so we can't assume that the input values are equal. See the linked issue for further discussion of possible fixes.

@emilyklemilykl self-assigned this Feb 19, 2026
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
@mosh3eb
mosh3ebforce-pushed the fix/add-vline-datetime-annotation branch 2 times, most recently from c580b1f to 9bed9d3CompareFebruary 20, 2026 23:34
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py
Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated
Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
Comment threadtests/test_optional/test_autoshapes/test_annotated_shapes.py Outdated

@emilyklemilykl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, @mosh3eb, looks good! 🎉 There's a lot of people that will be happy about this one.

@emilykl
emilykl merged commit dd9d2e5 into plotly:mainMay 8, 2026
23 of 24 checks passed
Sign up for freeto 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.

Error on using add_vline with text annotation for data with date-time x axis

2 participants

@mosh3eb@emilykl