[英]plot average value every 30 minutes
I am trying to plot the average value of "Spread" every 30 minutes.我正在尝试 plot 每 30 分钟“传播”的平均值。 My data look like this
我的数据看起来像这样
Bid Ask Spread relative_spread relative_quoted_half_spread
Date
2021-02-01 00:01:02 1.21291 1.21336 0.00045 0.000371 0.000185
2021-02-01 00:01:21 1.21290 1.21336 0.00046 0.000379 0.000190
2021-02-01 00:01:31 1.21287 1.21336 0.00049 0.000404 0.000202
2021-02-01 00:01:32 1.21290 1.21336 0.00046 0.000379 0.000190
2021-02-01 00:02:08 1.21290 1.21338 0.00048 0.000396 0.000198
My first attempt is我的第一次尝试是
plt.figure('Average spread over time')
minutes_dfs = df.resample('30M').mean()
print(Date)
ax3 = minutes_dfs.plot(x='Date', y='Spread',ls='-',color='k')
ax3.set_title('Spread introday every 30mins')
ax3.set_ylabel('Spread change')
ax3.legend(loc='best')
ax3.grid(True)
plt.show()
I got an error message as "Exception has occurred: KeyError 'Date'" However, I have tried this before I plot我收到一条错误消息,因为“发生了异常:KeyError 'Date'” 但是,我在 plot 之前尝试过这个
print(Date)
I got the Date column results like this我得到了这样的日期列结果
0 2021-02-01 00:01:02
1 2021-02-01 00:01:21
2 2021-02-01 00:01:31
3 2021-02-01 00:01:32
4 2021-02-01 00:02:08
My second try is我的第二次尝试是
plt.figure('Average spread over time')
minutes_dfs = df.groupby(pd.Grouper(key = 'Date', freq='30M')).mean()
print(Date)
ax3 = minutes_dfs.plot(x='Date', y='Spread',ls='-',color='k')
ax3.set_title('Spread introday every 30mins')
ax3.set_ylabel('Spread change')
ax3.legend(loc='best')
ax3.grid(True)
plt.show()
and then I got an error message like this 'The grouper name Date is not found'然后我收到这样的错误消息“找不到石斑鱼名称日期”
Does anyone know where did I go wrong?有谁知道我 go 哪里错了? Thanks in advance!
提前致谢!
Because Date
is index need remove x
:因为
Date
是索引需要删除x
:
x3 = minutes_dfs.plot(y='Spread',ls='-',color='k')
Or add reset_index
:或添加
reset_index
:
x3 = minutes_dfs.reset_index().plot(x='Date', y='Spread',ls='-',color='k')
In second solution is removed key
parameter, because DatetimeIndex
:在第二个解决方案中删除了
key
参数,因为DatetimeIndex
:
minutes_dfs = df.groupby(pd.Grouper(freq='30M')).mean()
x3 = minutes_dfs.plot(y='Spread',ls='-',color='k')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.