简体   繁体   English

熊猫中的KeyError

[英]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
...

It seems you need remove column Date by drop : 看来你需要删除列Datedrop

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

Or: 要么:

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

Or maybe: 或者可能:

del dta["Date"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM