Black Lives Matter. Please consider donating to Black Girls Code today.

TypeError: Object of type 'Time' is not JSON serializable

                  time             val1mw
0   00:00:00.000000000    11.1
1   00:01:00.000000000    12.1
2   00:02:00.000000000    13.1
3   00:03:00.000000000    14.1
4   00:04:00.000000000    15.1
5   00:05:00.000000000    16.1
6   01:00:00.000000000   131.1
7   01:01:00.000000000   132.1
8   01:02:00.000000000   133.1
9   01:03:00.000000000   134.1
10  01:04:00.000000000   135.1
11  01:05:00.000000000   136.1
12  02:00:00.000000000   251.1
13  02:01:00.000000000   252.1
14  02:02:00.000000000   253.1
15  02:03:00.000000000   254.1
16  02:04:00.000000000   255.1
17  02:05:00.000000000   256.1

This is my pandas dataframe “df”, where time column is of type ‘Time’, and val1mw column with data type ‘float64’.

fig = px.scatter(data_frame=df, x="time", y="val1mw")
fig.show()

As I tried to plot a graph of time and value as in the above code , I am getting the following error.

Traceback (most recent call last):
  File "/home/superusr/PycharmProjects/SRLDC/query1.py", line 72, in <module>
    fig.show()
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/plotly/basedatatypes.py", line 2783, in show
    return pio.show(self, *args, **kwargs)
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/plotly/io/_renderers.py", line 391, in show
    renderers._perform_external_rendering(fig_dict, renderers_string=renderer, **kwargs)
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/plotly/io/_renderers.py", line 340, in _perform_external_rendering
    renderer.render(fig_dict)
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/plotly/io/_base_renderers.py", line 734, in render
    validate=False,
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/plotly/io/_html.py", line 138, in to_html
    fig_dict.get("data", []), cls=utils.PlotlyJSONEncoder, sort_keys=True
  File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/_plotly_utils/utils.py", line 44, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/home/superusr/PycharmProjects/SRLDC/venv/lib/python3.6/site-packages/_plotly_utils/utils.py", line 113, in default
    return _json.JSONEncoder.default(self, obj)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Time' is not JSON serializable

I am not able to find a solution for this. Please give me an efficient solution.

Hi @reddihareesh welcome to the forum. Could you please tell us which version of plotly.py you are using, and print df['time'].dtype? I could not reproduce the issue with the latest version of plotly.

Hi @Emmanuelle Thank you. The version I am using is 4.3.0. and data type of time is object

import plotly
plotly.__version__

'4.3.0'


print(df['time'].dtype)

object

Actually I am getting the same kind of error for Date type as well
TypeError: Object of type 'Date' is not JSON serializable

for data frame shown below and date is also of type object

           date                time  val1mw
0    2017-02-01  00:00:00.000000000    91.1
1    2017-02-01  00:01:00.000000000    92.1
2    2017-02-01  00:02:00.000000000    93.1
3    2017-02-01  00:03:00.000000000    94.1
4    2017-02-01  00:04:00.000000000    95.1

Hello plotly , can any one give me a solution?