Python 熊猫错误:“;pandas.hashtable.PyObjectHashTable.get“项目”;

Python 熊猫错误:“;pandas.hashtable.PyObjectHashTable.get“项目”;,python,pandas,Python,Pandas,我在Python3.4.2上看到了Pandas 0.15.2的一些奇怪行为 首先,我导入的数据没有任何问题: 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') 但在尝试访问时,我遇到了一些奇怪的行为。这很好: In[289]:

我在Python3.4.2上看到了Pandas 0.15.2的一些奇怪行为

首先,我导入的数据没有任何问题:

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')
但在尝试访问时,我遇到了一些奇怪的行为。这很好:

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
但这失败了:

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'
[290]中的
:每周['2013-12-29']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
每周['2013-12-29']
文件“C:\Anaconda\envs\py34\lib\site packages\pandas\core\frame.py”,第1780行,位于\uu getitem中__
返回self.\u getitem\u列(键)
文件“C:\Anaconda\envs\py34\lib\site packages\pandas\core\frame.py”,第1787行,在\u getitem\u列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\Anaconda\envs\py34\lib\site packages\pandas\core\generic.py”,第1068行,在\u get\u item\u缓存中
values=self.\u data.get(项目)
get中的文件“C:\Anaconda\envs\py34\lib\site packages\pandas\core\internals.py”,第2849行
loc=自身项目。获取loc(项目)
文件“C:\Anaconda\envs\py34\lib\site packages\pandas\core\index.py”,第1402行,在get\u loc中
返回self.\u引擎。获取\u loc(\u值\u来自\u对象(键))
pandas.index.IndexEngine.get_loc(pandas\index.c:3807)中的文件“pandas\index.pyx”,第134行
文件“pandas\index.pyx”,第154行,在pandas.index.IndexEngine.get_loc(pandas\index.c:3687)中
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12310)中第696行的文件“pandas\hashtable.pyx”
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12261)中第704行的文件“pandas\hashtable.pyx”
关键错误:“2013-12-29”
有什么想法吗?这也会失败:weekly[weekly.index[0]],而且似乎不应该

数据如下:


谢谢。

您想使用
.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


这是一个奇怪的错误(我建议它看起来确实像一个bug),通常我会尽量避免使用
weekly[..]
符号,除了访问列之外,因为它超载了很多…

我打开了一个bug报告,得到了以下响应:

请参见此处的文档:

部分字符串索引仅适用于切片,否则将尝试列索引 挑选


希望这能帮助未来迷茫的人

谢谢,但是。loc也给了我错误!weekly.loc['2012-12-30']工作正常,但weekly.loc['2012-12-30','2012-12-30']失败@sergeyf使用字符串表示日期是非常不可靠的。。。。我喜欢使用Timestamps
weekly.loc[pd.to_datetime(['2012-12-30','2012-12-30'])]
(这很有效)。@Andy Hayden这是部分字符串索引;不适用于纯标量切片(且loc是严格的)