Pandas index.get_loc giving keyerror for time series data

Arun Kamath picture Arun Kamath · Mar 25, 2018 · Viewed 12.7k times · Source

The following code works fine.

import pandas as pd
#import numpy as np 
 import matplotlib.pyplot as plt 
 from IPython import get_ipython 
 get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SPY' 
df_close = pd.DataFrame() 
df_temp = pd.read_json('https://api.iextrading.com/1.0/stock/'+sym+'/chart/5y') 
df_temp.set_index('date',inplace=True) 
df_close = df_temp['close']

loc = df_close.index.get_loc('2015-08-17')

I modified it to fetch data from nsepy package.i.e. replaced read_json line and commented set_index line as the data fetched from the package has the date line as index by default

import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
from datetime import date
from nsepy import get_history

get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SBIN'
df_close = pd.DataFrame()
df_temp = get_history(symbol=sym,
                   start=date(2014,1,1),
                   end=date(2018,3,24))
#df_temp.set_index('date',inplace=True)
df_close = df_temp['Close']

loc = df_close.index.get_loc('2015-08-17')

In both cases, df_close is a series and they have the date present in them. Only difference is that in the correct scenario it contains date in format like 2013-03-25 00:00:00

Whereas in the incorrect one it is in format like 2014-01-01

Here is the log.

runfile('C:/Users/Arun/.spyder-py3/Practise files/market_correction.py', wdir='C:/Users/Arun/.spyder-py3/Practise files') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Arun/.spyder-py3/Practise files/market_correction.py', wdir='C:/Users/Arun/.spyder-py3/Practise files')

File "C:\Users\Arun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Arun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Arun/.spyder-py3/Practise files/market_correction.py", line 27, in loc = df_close.index.get_loc('2015-08-17')

File "C:\Users\Arun\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key))

File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item

File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: '2015-08-17'

What am I doing wrong? The day is present in the series.

I have tried df.loc method as well, but that gives other errors.

I am using anaconda spyder with python 3.6

Solution :

import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
from datetime import date
from nsepy import get_history

get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SBIN'
df_close = pd.DataFrame()
df_temp = get_history(symbol=sym,
                   start=date(2014,1,1),
                   end=date(2018,3,24))

**df_temp.reset_index(drop = False, inplace = True)
df_temp['Date']= pd.to_datetime(df_temp['Date'])
df_temp.set_index('Date',inplace=True)**
df_close = df_temp['Close']

loc = df_close.index.get_loc('2015-08-17')

Answer

jezrael picture jezrael · Mar 25, 2018

I think need set_index and maybe convert to datetimes, because KeyError means there is no value 2015-08-17 in index:

#check if DatetimeIndex
print (df_temp.index)

#if necessary convert column to index
df_temp.set_index('date',inplace=True) 
#if necessary convert to datetimes
df_temp.index= pd.to_datetime(df_temp.index)

loc = df_temp.index.get_loc('2015-08-17')