question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError in datetime pandas when accessing with datetime.date

See original GitHub issue

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

And here the errors:

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrockmendelcommented, Nov 24, 2020

Can you post a copy/paste-able example https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

Best guess is that it isn’t Big Sur, but that you recently updated to a newer version of pandas. There have been a few issues involving datetime.date objects recently. You’re safest bet is to always use datetime.datetime or pd.Timestamp objects instead.

0reactions
diprajkadlagcommented, Mar 5, 2022

Can you post a copy/paste-able example https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

Best guess is that it isn’t Big Sur, but that you recently updated to a newer version of pandas. There have been a few issues involving datetime.date objects recently. You’re safest bet is to always use datetime.datetime or pd.Timestamp objects instead.

datetime.datetime(2016,1,1) worked for me, while I was trying datetime.date(2016,1,1). My pandas version is 1.4.1. As soon I updated my pandas library this error started showing up. New versions of pandas accept datetime.datetime rather than datetime.date!

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas dataframe index datetime.date converts to object ...
I really would like to use datetime.date because it describes my data best. My pandas version is 0.17.0. I am using python 3.5.0....
Read more >
Time Series / Date functionality — pandas 0.17.0 documentation
Under the hood, pandas represents timestamps using instances of Timestamp and sequences of timestamps using instances of DatetimeIndex.
Read more >
How to handle KeyError : 'date'? - Mystery Errors - Django Forum
Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name… Before doing ......
Read more >
KeyError: 'Date' With component with date range picker
This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use...
Read more >
KeyError Pandas – How To Fix - Data Independent
KeyError Pandas – How To Fix · Preferred Option: Make sure that your column label (or row label) is in your dataframe! ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found