pandas中的KeyError

时间:2017-06-06 08:33:51

标签: python pandas

我试图弄清楚一个时间序列的例子。 Here is the link

我在这段代码中有错误:

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

错误如下所示:

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'

为什么它不起作用的任何建议?

对原始数据的引用不起作用,但我找到了所有源数据。输入数据示例:

    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
...

1 个答案:

答案 0 :(得分:2)

您似乎需要按drop删除列onChanges (0): name set to "Alice", onInit, doCheck, afterContentInit, afterContentChecked, afterViewInit, afterViewChecked, doCheck, afterContentChecked, afterViewChecked, onChanges (1): name changed to "Bob", doCheck, afterContentChecked, afterViewChecked, onDestroy--app, onDestroy--child onChanges (0): name set to "Alice" onInit doCheck afterContentInit afterContentChecked afterViewInit afterViewChecked doCheck afterContentChecked afterViewChecked onChanges (1): name changed to "Bob" doCheck afterContentChecked afterViewChecked onDestroy--app

Date

或者:

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

或者也许:

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