Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gantt chart docs - TypeError: Object of type timedelta is not JSON serializable #4145

Closed
helen-fornazier opened this issue Apr 3, 2023 · 10 comments

Comments

@helen-fornazier
Copy link

Hi,

I am following this docs to generate a gantt chart https://github.com/plotly/plotly.py/blob/doc-prod/doc/python/gantt.md

But this is what I get:

Traceback (most recent call last):
  File "/home/ccu/ccu/mesa-performance-tracking/mesa-ci-status/gantt-chart-test.py", line 12, in <module>
    fig.show()
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/basedatatypes.py", line 3390, in show
    return pio.show(self, *args, **kwargs)
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/io/_renderers.py", line 403, in show
    renderers._perform_external_rendering(fig_dict, renderers_string=renderer, **kwargs)
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/io/_renderers.py", line 340, in _perform_external_rendering
    renderer.render(fig_dict)
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/io/_base_renderers.py", line 748, in render
    html = to_html(
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/io/_html.py", line 144, in to_html
    jdata = to_json_plotly(fig_dict.get("data", []))
  File "/home/ccu/.local/lib/python3.10/site-packages/plotly/io/_json.py", line 123, in to_json_plotly
    return json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts)
  File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/ccu/.local/lib/python3.10/site-packages/_plotly_utils/utils.py", line 59, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/ccu/.local/lib/python3.10/site-packages/_plotly_utils/utils.py", line 136, in default
    return _json.JSONEncoder.default(self, obj)
  File "/usr/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type timedelta is not JSON serializable

I installed with pip, and the version is the same indicated on top here https://plotly.com/python/gantt/

ccu@ginger ~/c/mesa (cuttlefish)> pip show plotly
Name: plotly
Version: 5.14.0
Summary: An open-source, interactive data visualization library for Python
Home-page: https://plotly.com/python/
Author: Chris P
Author-email: chris@plot.ly
License: MIT
Location: /home/ccu/.local/lib/python3.10/site-packages
Requires: packaging, tenacity
Required-by: 

So I'm wondering if this is a bug or if I'm doing something wrong, or maybe the docs is outdated (and I can help with that once I figure the right way to implement it).

Thanks.

@AaronStiff
Copy link
Collaborator

@helen-fornazier which example in the docs specifically generated the error? I was able to run the first few examples successfully with the same version as you.

@helen-fornazier
Copy link
Author

the first example:

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()

and just for information:

$python --version
Python 3.10.6

@helen-fornazier
Copy link
Author

All the first examples before Deprecated Figure Factory gives me this error.

The Deprecated Figure Factory, Group Tasks Together and Color by Numeric Variable works, but not the ones under Gantt Charts and Timelines with plotly.express section

@aberres
Copy link

aberres commented Apr 4, 2023

@helen-fornazier Are you using Pandas 2? Then the fix might be here: #4103

@arg0d
Copy link

arg0d commented Apr 4, 2023

I'm experiencing the same issue.

$ pip freeze | grep pandas
pandas==2.0.0

Which version of pandas should we use?

@aberres
Copy link

aberres commented Apr 4, 2023

@arg0d A downgrade to 1.5.3 should help.

pip install pandas==1.5.3

@helen-fornazier
Copy link
Author

Yes, I was using pandas==2.0.0, downgrading with pip install pandas==1.5.3 works. Thanks.

@kaibir
Copy link
Contributor

kaibir commented Apr 5, 2023

Since #4103 has been merged about an hour ago (and closed #4102), this one here can be closed too, can't it? @helen-fornazier

@alexcjohnson
Copy link
Collaborator

@helen-fornazier would you try again with the new v5.14.1 and pandas v2.0.0 and let us know if the issue is resolved? Thanks!

@helen-fornazier
Copy link
Author

@helen-fornazier would you try again with the new v5.14.1 and pandas v2.0.0 and let us know if the issue is resolved? Thanks!

It works! Looks solved. Thanks! Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants