I’m trying to get the SP500 index returns of all US presidents but it seems something is going wrongly with the pct_change() method as I am not sure on how to ‘discriminate’ presidents for which there is no SP500 records. Below you can see the code I’m trying to implement:
presidents['Start'] = presidents['Start'].apply(lambda x: datetime.strptime(x, "%B %d, %Y").strftime("%Y-%m-%d"))
presidents['End'] = presidents['End'][:-1].apply(lambda x: datetime.strptime(x, "%B %d, %Y").strftime("%Y-%m-%d"))
presidents['End'].iloc[-1] = datetime.now().strftime("%Y-%m-%d")
cumulative_returns = {}
for index, row in presidents.iterrows():
party = row['Party']
president = row['Name']
start_date = row['Start']
end_date = row['End']
sp500_data = yf.download('^GSPC', start_date, end_date, progress=False)['Adj Close']
returns = sp500_data.pct_change()
c_ret = (1 + returns).cumprod().fillna(axis=1)
cumulative_returns[president] = {'cumulative_returns': c_ret, 'party': party}
This is what I get when executing the code:
returns = sp500_data.pct_change()
^^^^^^^^^^^^^^^^^^^^^^^
ValueError: attempt to get argmax of an empty sequence
If someone could point out what is happening, it is highly appreciated.
Thanks in advance.
ADV_1977 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1