Python 熊猫数据读取器:规范化日期
我使用熊猫数据阅读器包从像fred、yahoo finance这样的网站上提取经济时间序列。我从“fred”网站上下载了《美国经济衰退》(USREC)系列,从雅虎财经(yahoo finance)网站下载了历史sp500(^GSPC) 美国历史性衰退:Python 熊猫数据读取器:规范化日期,python,pandas,pandas-datareader,Python,Pandas,Pandas Datareader,我使用熊猫数据阅读器包从像fred、yahoo finance这样的网站上提取经济时间序列。我从“fred”网站上下载了《美国经济衰退》(USREC)系列,从雅虎财经(yahoo finance)网站下载了历史sp500(^GSPC) 美国历史性衰退: web.DataReader("USREC", "fred", start, end) 输出: 2017-08-01 0 2017-09-01 0 2017-10-01 0 2017-11-01 0 2
web.DataReader("USREC", "fred", start, end)
输出:
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-08-31 2456.0
2017-09-30 2493.0
2017-10-31 2557.0
2017-11-30 2594.0
标准普尔500指数回归
web.DataReader("^GSPC",'yahoo',start,end)['Close'].to_frame().resample('M').mean().round()
输出:
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-08-31 2456.0
2017-09-30 2493.0
2017-10-31 2557.0
2017-11-30 2594.0
我想合并这两个数据帧,但其中一个具有该月的开始日期,另一个具有该月的结束日期。如何使a)日期列yyyy mm b)使两个帧的日期列均为月初或月末
谢谢你的帮助 您可以使用
MS
在月初进行重采样:
web.DataReader("^GSPC",'yahoo',start,end)['Close'].to_frame().resample('MS').mean().round()
或者可以用于月份周期索引:
df1 = df1.to_period('M')
df2 = df2.to_period('M')
print (df1)
Close
2017-08 0
2017-09 0
2017-10 0
2017-11 0
print (df2)
Close
2017-08 2456.0
2017-09 2493.0
2017-10 2557.0
2017-11 2594.0
print (df1.index)
PeriodIndex(['2017-08', '2017-09', '2017-10', '2017-11'], dtype='period[M]', freq='M')
print (df2.index)
PeriodIndex(['2017-08', '2017-09', '2017-10', '2017-11'], dtype='period[M]', freq='M')