我正在尝试绘制折线图。下面是我的代码
代码:
import plotly.offline as pyo
import plotly.graph_objects as go
flag = determineFlag('2020-03-01','2020-03-30')
df_r = getDataForTrend(df,'2020-03-01','2020-03-30','d')
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
data = [go.Scatter(x = df_r[df_r['S2PName-Category']==category]['S2BillDate'],
y = df_r[df_r['S2PName-Category']==category]['totSale'],
mode = 'lines',
name = category) for category in df_r['S2PName-Category'].unique()]
layout = {'title':'Category Trend',
'xaxis':{'title':'Time Frame'},
'yaxis':{'title':'Total Sales Amount','tickformat' : '.2f'}}
fig = go.Figure(data=data,layout=layout)
pyo.iplot(fig)
当我运行上面的代码时,出现以下错误:
错误:
TypeError: Object of type Period is not JSON serializable
在调试时,我尝试执行以下代码
调试代码:
df_r[df_r['S2PName-Category']==category]['S2BillDate']
OP:
3 2020-03-01
11 2020-03-02
21 2020-03-03
26 2020-03-04
41 2020-03-06
42 2020-03-05
46 2020-03-07
Name: S2BillDate, dtype: period[D]
如何修复类型错误?这有什么调整吗?任何帮助深表感谢!谢谢!
据我所知,这仍然是一个问题,并且在这种情况下会失败。 github 上仍然存在一个未解决的问题:支持将 Pandas 时间跨度作为索引 col。
正如评论中所建议的,解决方案之一是使用
to_timestatmp
转换,请参阅 this。
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.offline as pyo
df = pd.DataFrame({"one": np.random.random(10)}, index=pd.period_range("2020-03-01", freq="M", periods=10))
# Out:
# one
# 2020-03 0.302359
# 2020-04 0.919923
# 2020-05 0.673808
# 2020-06 0.718974
# 2020-07 0.754675
# 2020-08 0.731673
# 2020-09 0.772382
# 2020-10 0.654555
# 2020-11 0.549314
# 2020-12 0.101696
data = [
go.Scatter(
x=df.index,
y=df["one"],
)
]
fig = go.Figure(data=data)
pyo.iplot(fig)
# -> will fail ("TypeError: Object of type Period is not JSON serializable")
to_timestamp
转换 (-> df.index.to_timestamp()
)data = [
go.Scatter(
x=df.index.to_timestamp(), # period to datetime conversion
y=df["one"],
)
]
fig = go.Figure(data=data)
pyo.iplot(fig)
或者,如果您不需要日期时间格式,也可以将其转换为字符串:
data = [
go.Scatter(
x=df.index.strftime("%Y-%m"), # You can define your own format
y=df["one"],
)
]
fig = go.Figure(data=data)
pyo.iplot(fig)
您当然可以在原始数据帧上进行此转换(因此您不需要在
go.Scatter
内迭代执行此转换),但这只是小事。可能还有一种使用自定义编码器而不是默认编码器的方法,但我认为这不值得尝试,而且据我所知,没有比使用从 Period
到 datetime
或 string
的可能转换之一更好的解决方案了。