Px.timeline converts x-axis to unusable timedelta type

Even if I just copy and paste the example code from the timeline page, I get:

TypeError: Object of type timedelta is not JSON serializable.

Example code:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()

From my investigation, I’ve found that plotly seems to be setting the x-axis to a timedelta object. I’m currently running the most recent version of plotly and pandas.

2 Likes

I’ve encountered the same problem

Also experiencing the same issue.

Hi @devinguthrie, did you try the above posted code?

I do not get any error or warning using plotly version 5.18.0.

I did try the sample code posted above when I was trying to get this to work. Do to the low views of this post and lack of other post around the web, I assumed it was a problem isolated to python and a specific version of the package. I started on plotly==5.5.0 I tried down grading to when px.timeline() was introduced in plotly=4.9.0 with pip install plotly==4.9.0 --force-reinstall. I restarted the kernel and still got the same error. So I updated to the latest plotly==5.18.0 restarted my kernel and it worked.

1 Like