Search code examples
pythonpandasdataframemacos-big-suryfinance

KeyError in datetime pandas due to Big Sur?


I've just upgrade Big Sur 11.0.1 and I'm facing some issue while trying to execute a notebook. Weird because it worked on Catalina. Could anyone help ?

import numpy as np
import pandas as pd
import yfinance as yf


# In[56]:


#parameters
RISKY_ASSET = 'ADBE'
START_DATE = '2017-01-01'
END_DATE = '2020-07-31'


# In[57]:


#Download data:
df = yf.download(RISKY_ASSET, start=START_DATE, end=END_DATE, adjusted=True)


# In[58]:


#daily return
adj_close = df['Adj Close']
returns = adj_close.pct_change().dropna()
print(f'Average return: {100 * returns.mean():.2f}%')
returns.plot(title=f'{RISKY_ASSET} returns: {START_DATE} - {END_DATE}')


# In[59]:


#Split the data into training and test sets:
train = returns['2017-01-01':'2020-06-30']
test = returns['2020-07-01':'2020-07-31']


# In[60]:


#parameters of the simulation:
T = len(test)
N = len(test)
S_0 = adj_close[train.index[-1].date()]

N_SIM = 1000
mu = train.mean()
sigma = train.std()

Error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-9316c025c623> in <module>
      2 T = len(test)
      3 N = len(test)
----> 4 S_0 = adj_close[train.index[-1].date()]
      5 N_SIM = 1000
      6 mu = train.mean()

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
    880 
    881         elif key_is_scalar:
--> 882             return self._get_value(key)
    883 
    884         if is_hashable(key):

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
    987 
    988         # Similar to Index.get_value, but we do not fall back to positional
--> 989         loc = self.index.get_loc(label)
    990         return self.index._get_values_for_loc(self, loc, label)
    991 

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
    620         else:
    621             # unrecognized type
--> 622             raise KeyError(key)
    623 
    624         try:

KeyError: datetime.date(2019, 12, 31) ```

Solution

  • The issue is actually due to the version of my Pandas. I have downgraded it to 1.0.5 and everything works know ! In a command terminal: pip install pandas == 1.0.5