Feb-11-2018, 11:21 PM
Dear Python Users,
I am trying to find annual return of S&P500 given monthly prices. I have the following code below. The problem is that I receive empty (Nan) observations. Can you please point at the mistakes that I am making? I attach csv file to this post.
I am trying to find annual return of S&P500 given monthly prices. I have the following code below. The problem is that I receive empty (Nan) observations. Can you please point at the mistakes that I am making? I attach csv file to this post.
import pandas as pd import numpy as np from datetime import datetime sp_500=pd.DataFrame(pd.read_csv("sp500_historical.csv")) sp_500['date'] = pd.to_datetime(sp_500['date']) logret = np.log(sp_500['Close'] / sp_500['Close'].shift(1)) date=[] d0=sp_500.date for i in range(0,len(logret)): date.append(d0[i].strftime("%Y")) y=pd.DataFrame(logret,date,columns=['ret_annual']) ret_annual=np.exp(y.groupby(y.index).sum())-1 ret_annual.head()
Attached Files
sp500_historical.csv (Size: 20.24 KB / Downloads: 51)