我试图做一个简单的时间序列图,显示muni掉期指数随时间的每周指数值。我无法将图表中的日期显示为日期。
%pylab inline
import pandas as pd
import numpy as np
import matplotlib as plt
import datetime as dt
ind = pd.read_csv("muni-swap-historical-data.csv")
ind.head(10)
Date IndexValue 7/5/1989 6.06 7/12/1989 5.77 7/19/1989 5.93 7/26/1989 6.09 8/2/1989 5.91 8/9/1989 5.94 8/16/1989 6.17 8/23/1989 6.19 8/30/1989 6.15 9/6/1989 6.03
ind_clean = ind.copy()
ind_clean['Date'] = pd.to_datetime(ind_clean['Date'])
ind_clean['Date'] = ind_clean['Date'].astype(dt.datetime)
ind_clean['Date'].head(10)
0 1989-07-05 00:00:00 1 1989-07-12 00:00:00 2 1989-07-19 00:00:00 3 1989-07-26 00:00:00 4 1989-08-02 00:00:00 5 1989-08-09 00:00:00 6 1989-08-16 00:00:00 7 1989-08-23 00:00:00 8 1989-08-30 00:00:00 9 1989-09-06 00:00:00
ind_clean.plot(title='KBW BKX Index Timeseries').legend(loc=6,
bbox_to_anchor=(1, 0.5))
绘图输出:
***编辑:问题是由'IndexValue'列的命名引起的。将此更改为csv文件中的“值”可解决问题。工作代码如下。
%pylab inline
import pandas as pd
import numpy as np
import matplotlib as plt
#import datetime as dt
ind = pd.read_csv("muni-swap-historical-data.csv")
ind['Date'] = pd.to_datetime(ind['Date'])
#unnecessary ind['Date'] = ind_clean['Date'].astype(dt.datetime)
ind.plot(x='Date', y='Value', title='SIFMA Muni Swap Index Timeseries').legend(loc=6, bbox_to_anchor=(1, 0.5))