🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Error if adding annotation to add_vline on plotly express when x is a datetime

I’m getting the following error when I try to annotate a vline using the “annotate_text” argument in the “add_vline” method:

TypeError: unsupported operand type(s) for +: 'int' and 'datetime.datetime'

The x-value is a datetime.datetime type. If I change the x to float or int, it does not give the error. The behavior is the same on the “add_hline” method.

Full code and error message:

1 Like

Dug into a bit, was able to get it to work by moving the “_mean” function calls to just before they are needed in the hline and vline blocks. Obviously this is not ideal because it’d require changing the call in multiple places (and would not address angled lines from what I understand of the code).
It looks like (2) values are being passed in for all uses in this file, so would finding the point that is equidistant between the x0 and x1 values provide the expected results?

1 Like

i have the same issue and dont want to fiddle plotly side code!
err in

return float(sum(x)) / len(x)
in file /plotly/shapeannotation.py", line 7, in _mean

can we get a fix?

interestingly Horizontal and Vertical Lines and Rectangles | Python | Plotly the fig.add_vrect is ok with x0 x1 datetime objects

We’ve started adding a trace to do the same thing, which has some other benefits (can add to legend, hover info, etc).
I could put in a PR assuming that mean is finding the half way point, just need to find some time between work and prepping the best for the new bebs.

Same problem here. Tried to use datetime64, datetime.datetime and datetime.date, but it raises the same exception when trying to add annotations.