Search code examples
pythonpandasdataframekeyerror

Adding the last line of code results in this error: "in get_loc raise KeyError(key) from err". What causes this error?


I am trying to subset the Dates from 2013 to 2018 and adding the last line of the code Code results in this error Error.

Why does this happen and can anyone tell me if there is a better way to subset the Dates ?

ERROR :

File "C:\Users\Dev\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Date'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Python Projects\MTE\Fitness Tracker\Analyze Your Runkeeper Fitness Data\datasets\Fitness Data.py", line 29, in <module>
    datesss=df_run[(df_run['Date'] > '01-01-2013') & (df_run['Date'] <= '31-12-2018')]
  File "C:\Users\Dev\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Dev\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'Date'

Solution

  • Your code looks correct, can you please check when you are creating df_run Dataframe from df_activities, it has 'Date' column and not index else you will have to reset_index().

    Error suggest that the DataFrame(df_run) either does not contain 'Date' column or it is set as index.

    Boolean mask can be used, works with date as object type or with TimeStamp type:

    Solution

    mask = (df['Date'] > '01-01-2013') & (df['Date'] <= '31-12-2019')
    df.loc[mask]