Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 550
peolss Avatar asked Aug 09 '18 15:08

peolss


People also ask

Is not JSON serializable?

The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON. To solve the error, make sure to call the function and serialize the object that the function returns.

What is JSON dump Python?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.


1 Answers

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.

like image 169
Upasana Mittal Avatar answered Oct 01 '22 01:10

Upasana Mittal