0

我刚刚升级了 Big Sur 11.0.1,在尝试执行笔记本时遇到了一些问题。很奇怪,因为它对 Catalina 有效。有人可以帮忙吗?

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()

错误:

---------------------------------------------------------------------------
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) ```
4

1 回答 1

0

这个问题实际上是由于我的熊猫版本。我已将其降级为 1.0.5,一切正常!在命令终端中: pip install pandas == 1.0.5

于 2020-11-22T16:31:31.267 回答