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

peolss picture peolss · Aug 9, 2018 · Viewed 37.7k times · Source

I'm trying to create a plotly graph with some data I've got from my PostgreSQL server, but when I try to graph I'm getting an error: "TypeError: Object of type 'DataFrame' is not JSON serializable"

Here's the code so far:

import dash
import numpy as np
import pandas as pd
import plotly.offline as py
import plotly.graph_objs as go
import psycopg2 as pg2
import datetime

conn = pg2.connect(database='X',user='X',password=secret)

cur = conn.cursor()

cur.execute("SELECT * FROM times;")
a = cur.fetchall()
str(a)


df = pd.DataFrame([[ij for ij in i] for i in a])
df.to_json()
df.rename(columns={0: "Serial Number", 1: "Status", 2: "Date", 3: "Time", 4: "Number"}, inplace=True);

x = df["Date"]
data = [go.Scatter(
            x=x,
            y=df["Status"])]

layout = go.Layout(title="Server Data Visualization",
                   xaxis = dict(
                   range = [df.head(1),
                            df.tail(1)]),
                    yaxis=dict(title = "Status"))

fig = go.Figure(data = data, layout = layout)
py.plot(fig)

The df["Date"] is the date in format of "2018-08-03" and the df["Status"] is either "Uptime" or "Downtime."

Can someone tell me what I'm doing incorrectly? I'm trying to have this graph basically be dates on the x-axis read in from the sql server, and then two values on the y-axis that represent either the value of "Uptime" or "Downtime."

Traceback (most recent call last):
  File "\\srv31data1\users$\User\Desktop\basic.py", line 37, in <module>
    py.plot(fig)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\offline\offline.py", line 469, in plot
    '100%', '100%', global_requirejs=False)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\offline\offline.py", line 184, in _plot_html
    cls=utils.PlotlyJSONEncoder)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\utils.py", line 161, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\utils.py", line 229, in default
    return _json.JSONEncoder.default(self, obj)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'DataFrame' is not JSON serializable

Edit: Sorry, forgot to post the traceback!

Answer

Upasana Mittal picture Upasana Mittal · Aug 9, 2018

Your df is still a data frame because you haven't assigned it as json.

   df = df.to_json()

This should work. Let me know if not.