繁体   English   中英

格式化日期时间以使用 Pandas 绘制时间序列

[英]Formatting datetime for plotting time-series with pandas

我正在用 Pandas 数据框绘制一些时间序列,但在周末遇到了间隔问题。 我该怎么做才能消除时间序列图中的空白?

date_concat = pd.to_datetime(pd.Series(df.index),infer_datetime_format=True)
pca_factors.index = date_concat
pca_colnames = ['Outright', 'Curve', 'Convexity']
pca_factors.columns = pca_colnames

fig,axes = plt.subplots(2)
pca_factors.Curve.plot(ax=axes[0]); axes[0].set_title('Curve')
pca_factors.Convexity.plot(ax=axes[1]); axes[1].set_title('Convexity'); plt.axhline(linewidth=2, color = 'g')
fig.tight_layout()
fig.savefig('convexity.png')

部分图如下: 在此处输入图片说明

理想情况下,我希望时间序列只显示工作日而忽略周末。

为了使 MaxU 的建议更加明确:

  • 像你所做的那样转换为日期时间,但放弃周末
  • 重置索引并通过此默认 Int64Index 绘制数据
  • 更改 x 刻度标签

代码:

date_concat = data_concat[date_concat.weekday < 5] # drop weekends
pca_factors = pca_factors.reset_index() # from MaxU's comment
pca_factors['Convexity'].plot()         # ^^^
plt.xticks(pca_factors.index, date_concat) # change the x tick labels

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM