英文:
Creating line chart out of timeseries dataframe gives ValueError in plotly
问题
我有一个时间序列的数据帧(虚拟数据),如下所示,我试图使用Plotly创建一条线图,将所有列的值放在y轴上,而索引是x轴。数据帧的所有列都具有相同数量的行,并且在检查索引的类型时,它是'dtype='datetime64[ns, pytz.FixedOffset(60)]'。
然而,按照下面的代码创建线图时,我收到以下错误消息:"ValueError: All arguments should have the same length. The length of argument y is 5, whereas the length of previously-processed arguments ['time_before_fulfilment'] is 109"。我查看了其他堆栈溢出答案并尝试了一些方法,但无法解决它。
有人可以帮忙吗?
# 用于创建虚拟数据帧的代码
data = {
'2001-07-21 10:00:00+05:00': [45, 51, 31, 3],
'2001-07-21 10:15:00+05:00': [46, 50, 32, 3],
'2001-07-21 10:30:00+05:00': [47, 51, 34, 7],
'2001-07-21 10:45:00+05:00': [50, 50, 33, 9]
}
# 创建数据帧
df = pd.DataFrame(data, index=['2001-07-21 10:45:00+05:00', 'Col2', 'Col3', 'Col4'])
df.index.name = 'date'
df = df.rename_axis(index=None, columns='date').T
df.index = pd.to_datetime(df.index, utc=True)
df.index = df.index.tz_convert(pytz.FixedOffset(60))
# 显示数据帧
df
代码
def plot_graph():
fig = px.line(df, x=df.index, y=[df.columns[0],'Col2','Col3','Col4'], markers='.')
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1H", step="hour", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
plot_graph()
英文:
I have a time series dataframe (dummy) as below, for which I am trying to create a line chart using plotly to plot the values of all the columns on y axis while index is the x axis. All the columns of the dataframe have the same number of rows and upon checking the type of the index, it is 'dtype='datetime64[ns, pytz.FixedOffset(60)]'
However, while creating the line chart as per code below, I get the following error: "ValueError: All arguments should have the same length. The length of argument y is 5, whereas the length of previously-processed arguments ['time_before_fulfilment'] is 109". I went through other stack overflow answers and tried a couple of things but couldn't solve it.
Could someone kindly help?
# Code to create dummy dataframe
data = {
'2001-07-21 10:00:00+05:00': [45, 51, 31, 3],
'2001-07-21 10:15:00+05:00': [46, 50, 32, 3],
'2001-07-21 10:30:00+05:00': [47, 51, 34, 7],
'2001-07-21 10:45:00+05:00': [50, 50, 33, 9]
}
# Create the DataFrame
df = pd.DataFrame(data, index=['2001-07-21 10:45:00+05:00', 'Col2', 'Col3', 'Col4'])
df.index.name = 'date'
df = df.rename_axis(index=None, columns='date').T
df.index = pd.to_datetime(df.index, utc=True)
df.index = df.index.tz_convert(pytz.FixedOffset(60))
# Show the DataFrame
df
Dataframe
2001-07-21 10:45:00+05:00 Col 2 Col 3 Col 4
date
2001-07-21 10:00:00+05:00 45 51 31 3
2001-07-21 10:15:00+05:00 46 50 32 3
2001-07-21 10:30:00+05:00 47 51 34 7
2001-07-21 10:45:00+05:00 50 50 33 9
Code
def plot_graph():
fig = px.line(df, x = df.index, y = [df.columns[0],'Col2','Col3','Col4'] , markers='.')
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count = 1, label = "1H", step = "hour", stepmode ="backward"),
dict(step="all")
])
)
)
fig.show()
plot_graph()
答案1
得分: 1
以下是翻译好的部分:
"我将回答这个问题,以便有类似问题的人可以获得一些见解。问题是列名 '2001-07-21 10:45:00+05:00' 是一个时间戳,需要转换为字符串类型。这样做可以解决问题,使得 Plotly 代码生成所需的折线图。
代码:
##将列名从时间戳类型转换为字符串类型
timestamp = pd.Timestamp(df_main.columns[0])
date_string = timestamp.strftime('%Y-%m-%d %H:%M:%S%z')
df = df.rename(columns={df.columns[0]: date_string})
df
"
英文:
I will answer this question, so that someone with a similar issue can get some insight. The problem was that the column name '2001-07-21 10:45:00+05:00' is a timestamp which needed to be converted to type str. Doing that fixed the issue and the plotly code generated the desired line graph
Code:
##Converting the column name from type timestamp to str
timestamp = pd.Timestamp(df_main.columns[0])
date_string = timestamp.strftime('%Y-%m-%d %H:%M:%S%z')
df= df.rename(columns={df.columns[0]: date_string})
df
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论