我正在使用内置的pandas DataReader从Fama-French数据库中下载数据。日期最初只是整数,其yyyymm
格式为:
import pandas.io.data as web
ff = web.DataReader("F-F_Research_Data_Factors", "famafrench")[0]
ff.head()
我想将索引转换为日期时间,其中日期是该月的最后一天。现在,我正在这样做:
ff.reset_index(inplace=True)
import calendar
def dateParser(dt):
yyyy = int(dt[0:4])
mm = int(dt[4:6])
dd = calendar.monthrange(yyyy,mm)[1] #last day of month
return pd.datetime(yyyy,mm,dd)
ff['date'] = ff['index'].astype(str).apply(dateParser)
ff.index = ff['date']
ff.drop(['index', 'date'], axis=1, inplace=True)
有没有更快/更优雅的方式来实现这一目标?例如,有没有一种方法可以dateParser
直接应用到索引(也许是就位),所以我不必reset_index
首先应用?
In [35]: ff = web.DataReader("F-F_Research_Data_Factors", "famafrench")[0]
In [36]: ff.head()
Out[36]:
1 Mkt-RF 2 SMB 3 HML 4 RF
192607 2.96 -2.30 -2.87 0.22
192608 2.64 -1.40 4.19 0.25
192609 0.36 -1.32 0.01 0.23
192610 -3.24 0.04 0.51 0.32
192611 2.53 -0.20 -0.35 0.31
In [38]: ff.index
Out[38]:
Int64Index([192607, 192608, 192609, 192610, 192611, 192612, 192701, 192702, 192703, 192704,
...
201407, 201408, 201409, 201410, 201411, 201412, 201501, 201502, 201503, 201504],
dtype='int64', length=1066)
In [39]: ff.index = pd.to_datetime(ff.index,format='%Y%m') + pd.offsets.MonthEnd()
In [40]: ff.index
Out[40]:
DatetimeIndex(['1926-07-31', '1926-08-31', '1926-09-30', '1926-10-31', '1926-11-30', '1926-12-31', '1927-01-31', '1927-02-28', '1927-03-31', '1927-04-30',
...
'2014-07-31', '2014-08-31', '2014-09-30', '2014-10-31', '2014-11-30', '2014-12-31', '2015-01-31', '2015-02-28', '2015-03-31', '2015-04-30'],
dtype='datetime64[ns]', length=1066, freq='M', tz=None)
In [41]: ff.head()
Out[41]:
1 Mkt-RF 2 SMB 3 HML 4 RF
1926-07-31 2.96 -2.30 -2.87 0.22
1926-08-31 2.64 -1.40 4.19 0.25
1926-09-30 0.36 -1.32 0.01 0.23
1926-10-31 -3.24 0.04 0.51 0.32
1926-11-30 2.53 -0.20 -0.35 0.31
请注意,实际上转换索引的速度更快,如下所示,因为格式具有快速路径。
pd.to_datetime(ff.index*100+1,format='%Y%m%d') + pd.offsets.MonthEnd()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句