TypeError:“DataFrame"类型的对象不是 JSON 可序列化的
问题描述
我正在尝试使用从我的 PostgreSQL 服务器获得的一些数据创建一个绘图图,但是当我尝试绘制图表时出现错误: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"
这是目前为止的代码:
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)
df["Date"] 是2018-08-03"格式的日期,df["Status"] 是Uptime"或Downtime".
The df["Date"] is the date in format of "2018-08-03" and the df["Status"] is either "Uptime" or "Downtime."
谁能告诉我我做错了什么?我试图让这个图表基本上是从 sql server 读取的 x 轴上的日期,然后是 y 轴上的两个值,代表正常运行时间"或停机时间"的值.
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 "\srv31data1users$UserDesktopasic.py", line 37, in <module>
py.plot(fig)
File "C:UsersUserAppDataLocalProgramsPythonPython36libsite-packagesplotlyofflineoffline.py", line 469, in plot
'100%', '100%', global_requirejs=False)
File "C:UsersUserAppDataLocalProgramsPythonPython36libsite-packagesplotlyofflineoffline.py", line 184, in _plot_html
cls=utils.PlotlyJSONEncoder)
File "C:UsersUserAppDataLocalProgramsPythonPython36libjson\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:UsersUserAppDataLocalProgramsPythonPython36libsite-packagesplotlyutils.py", line 161, in encode
encoded_o = super(PlotlyJSONEncoder, self).encode(o)
File "C:UsersUserAppDataLocalProgramsPythonPython36libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:UsersUserAppDataLocalProgramsPythonPython36libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:UsersUserAppDataLocalProgramsPythonPython36libsite-packagesplotlyutils.py", line 229, in default
return _json.JSONEncoder.default(self, obj)
File "C:UsersUserAppDataLocalProgramsPythonPython36libjsonencoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'DataFrame' is not JSON serializable
抱歉,忘记发布回溯!
解决方案
你的 df
仍然是一个数据框,因为你没有将它分配为 json.
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.
相关文章