Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas error: "pandas.hashtable.PyObjectHashTable.get_item"

Tags:

python

pandas

I'm seeing some odd behavior from Pandas 0.15.2 on Python 3.4.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

The data is here: https://github.com/sergeyf/Python_data_science_class/blob/master/code/data/fremont_bridge_data.csv

Thank you.

like image 430
sergeyf Avatar asked Jul 28 '26 23:07

sergeyf


2 Answers

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

Hope this helps future confused people!

like image 199
sergeyf Avatar answered Jul 30 '26 11:07

sergeyf


You want to use .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...

like image 31
Andy Hayden Avatar answered Jul 30 '26 11:07

Andy Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!