From 8f517d9ae691da1924a63ce340b97585fa4a39df Mon Sep 17 00:00:00 2001 From: MoMo Date: Sat, 14 Feb 2026 06:16:10 +0200 Subject: [PATCH] Fix add_vline and add_hline with datetime axes When using add_vline with annotation_text on datetime axes, plotly was crashing with a TypeError because the _mean() helper function tried to sum date strings. Since vlines have x0==x1 and hlines have y0==y1, we can just return the first value when sum() fails on non-numeric data. This fixes the issue while maintaining full backward compatibility. Fixes #3065 --- plotly/shapeannotation.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plotly/shapeannotation.py b/plotly/shapeannotation.py index a2323ed02d4..9f1e0f76821 100644 --- a/plotly/shapeannotation.py +++ b/plotly/shapeannotation.py @@ -4,7 +4,14 @@ def _mean(x): if len(x) == 0: raise ValueError("x must have positive length") - return float(sum(x)) / len(x) + # Handle non-numeric values (e.g., date strings) + # For vline/hline, x0==x1 and y0==y1, so returning first value is correct + try: + return float(sum(x)) / len(x) + except TypeError: + # If sum fails (e.g., for date strings), return the first value + # This works because for vlines x0==x1, and for hlines y0==y1 + return x[0] def _argmin(x):