2017-12-23 15 views
1

私はfred、yahoo financeのようなウェブサイトから経済的な時系列を引き出すためにpandasデータリーダーパッケージを使用します。私は、「fred」ウェブサイトとyahooファイナンスからの歴史的sp500(^ GSPC)から私たちに不況(USREC)シリーズを引っ張ってきました。Pandas DataReader:正規化の日付

歴史米国の景気後退:

web.DataReader("USREC", "fred", start, end) 

出力:

2017-08-31 2456.0 
2017-09-30 2493.0 
2017-10-31 2557.0 
2017-11-30 2594.0 

2017-08-01  0 
2017-09-01  0 
2017-10-01  0 
2017-11-01  0 

S & P500は

web.DataReader("^GSPC",'yahoo',start,end)['Close'].to_frame().resample('M').mean().round() 

出力を返します。

2つのデータフレームをマージしたいが、1つは月の開始日を持ち、もう1つは月の終了日を持つ。 a)日付の列をyyyy-mmにするにはどうすればよいですか?b)両方のフレームの月の開始または終了の日付列を作成しますか?

ありがとうございました!

答えて

2

あなたがヶ月の開始によってリサンプルのためMSを使用することができます。月PeriodIndexため

web.DataReader("^GSPC",'yahoo',start,end)['Close'].to_frame().resample('MS').mean().round() 

か、可能である使用to_period

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') 
関連する問題