簡體   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