yanadm
yanadm

Reputation: 707

KeyError in pandas

I'm trying to make out one example of time series. Here is the link

I have an error in this piece of code:

dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
del dta["YEAR"]

The error looks like this:

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\indexes\base.py in 
get_loc(self, key, method, tolerance)
   2133             try:
-> 2134                 return self._engine.get_loc(key)
   2135             except KeyError:

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)()

pandas\src\hashtable_class_helper.pxi in 
pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)()

pandas\src\hashtable_class_helper.pxi in 
pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)()

KeyError: 'YEAR'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-22-5287e8e754f5> in <module>()
      1 dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
----> 2 del dta["YEAR"]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in 
__delitem__(self, key)
   1640             # there was no match, this call should raise the appropriate
   1641             # exception:
-> 1642             self._data.delete(key)
   1643 
   1644         # delete from the caches

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in 
delete(self, item)
   3600         Delete selected item (items if non-unique) in-place.
   3601         """
-> 3602         indexer = self.items.get_loc(item)
   3603 
   3604         is_deleted = np.zeros(self.shape[0], dtype=np.bool_)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\indexes\base.py in 
get_loc(self, key, method, tolerance)
   2134                 return self._engine.get_loc(key)
   2135             except KeyError:
-> 2136                 return 
self._engine.get_loc(self._maybe_cast_indexer(key))
   2137 
   2138         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)()

pandas\src\hashtable_class_helper.pxi in 
pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)()

pandas\src\hashtable_class_helper.pxi in 
pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)()

KeyError: 'YEAR'

Any suggestions why it does not work?

The reference to the original data does not work, but I found all the source data. Example of input data:

    Date       Yearly Mean     Yearly Mean    Number of     Definitive/   
               Total Sunspot    Standard     Observations   Provisional
                  Number        Deviation                    Indicator   
0   2008-12-31     4.2             2.5          6644.0          1.0   
1   2007-12-31    12.6             2.7          6841.0          1.0
2   2006-12-31    24.7             3.5          6370.0          1.0
3   2005-12-31    45.8             4.7          7084.0          1.0
4   2004-12-31    65.3             5.9          6882.0          1.0
...

Upvotes: 1

Views: 7541

Answers (1)

jezrael
jezrael

Reputation: 862441

It seems you need remove column Date by drop:

dta = dta.drop('Date', axis=1, level=0)

Or:

dta = dta.drop('Date', axis=1)

Or maybe:

del dta["Date"]

Upvotes: 2

Related Questions