熊猫错误:“pandas.hashtable.PyObjectHashTable.get_item”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28310429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas error: "pandas.hashtable.PyObjectHashTable.get_item"
提问by sergeyf
I'm seeing some odd behavior from Pandas 0.15.2 on Python 3.4.2.
我在 Python 3.4.2 上看到 Pandas 0.15.2 的一些奇怪行为。
First, I import the data with no issues:
首先,我导入数据没有问题:
import pandas as pd
# import the data
hourly = pd.read_csv("fremont_bridge_data.csv", index_col='Date', parse_dates=True)
weekly = hourly.resample('w','sum')
But when trying to access, I run into some odd behavior. This works fine:
但是在尝试访问时,我遇到了一些奇怪的行为。这工作正常:
In[289]: weekly['2013-12']
Out[289]:
northbound southbound total
Date
2013-12-01 5053 5480 10533
2013-12-08 5432 5836 11268
2013-12-15 5381 5760 11141
2013-12-22 5182 5455 10637
2013-12-29 3118 3567 6685
And this fails:
这失败了:
In[290]: weekly['2013-12-29']
Traceback (most recent call last):
File "<ipython-input-290-96e181f8ff0a>", line 1, in <module>
weekly['2013-12-29']
File "C:\Anaconda\envs\py34\lib\site-packages\pandas\core\frame.py", line 1780, in __getitem__
return self._getitem_column(key)
File "C:\Anaconda\envs\py34\lib\site-packages\pandas\core\frame.py", line 1787, in _getitem_column
return self._get_item_cache(key)
File "C:\Anaconda\envs\py34\lib\site-packages\pandas\core\generic.py", line 1068, in _get_item_cache
values = self._data.get(item)
File "C:\Anaconda\envs\py34\lib\site-packages\pandas\core\internals.py", line 2849, in get
loc = self.items.get_loc(item)
File "C:\Anaconda\envs\py34\lib\site-packages\pandas\core\index.py", line 1402, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3807)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3687)
File "pandas\hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12310)
File "pandas\hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12261)
KeyError: '2013-12-29'
Any ideas? This also fails: weekly[weekly.index[0]], and seems like it shouldn't
有任何想法吗?这也失败了:weekly[weekly.index[0]],似乎不应该
The data is here: https://github.com/sergeyf/Python_data_science_class/blob/master/code/data/fremont_bridge_data.csv
数据在这里:https: //github.com/sergeyf/Python_data_science_class/blob/master/code/data/fremont_bridge_data.csv
Thank you.
谢谢你。
回答by Andy Hayden
You want to use .loc:
你想使用.loc:
In [11]: weekly.loc['2013-12-29']
Out[11]:
Fremont Bridge NB 3118
Fremont Bridge SB 3567
Name: 2013-12-29 00:00:00, dtype: float64
This is a bizarre error (it does look like a bug, I recommend filing this on github), generally I try and avoid using the weekly[..]notation except for accessing columns as it's overloaded quite a bit...
这是一个奇怪的错误(它确实看起来像一个错误,我建议将其提交到 github 上),通常我会尽量避免使用该weekly[..]符号,除非访问列,因为它已经过载了很多......
回答by sergeyf
I opened a bug report, and got this response:
我打开了一个错误报告,并得到了这样的回应:
see docs here: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#datetimeindex-partial-string-indexing
partial string indexing is for slices only otherwise it tried column selection
请参阅此处的文档:http: //pandas.pydata.org/pandas-docs/stable/timeseries.html#datetimeindex-partial-string-indexing
部分字符串索引仅用于切片,否则它会尝试列选择
Hope this helps future confused people!
希望这可以帮助未来困惑的人!

