Pandas错误:“Pandas.hashtable.PyObjectHashTable.get_item”

2024-06-11 19:08:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python 3.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'

有什么想法吗?这也失败了:weekly[weekly.index[0]],看起来不应该

数据如下: https://github.com/sergeyf/Python_data_science_class/blob/master/code/data/fremont_bridge_data.csv

谢谢你。


Tags: inpandasdatagetindexliblineanaconda
2条回答

我打开了一个错误报告,得到了这样的答复:

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

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

要使用.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,我建议使用filing this on github),通常我会尝试避免使用weekly[..]符号,除了访问列之外,因为它超载了很多。。。

相关问题 更多 >