Skip to content

Access DateTimeIndexed dataframe by timestamp #3925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chinux23 opened this issue Jun 16, 2013 · 3 comments
Closed

Access DateTimeIndexed dataframe by timestamp #3925

chinux23 opened this issue Jun 16, 2013 · 3 comments
Labels
Datetime Datetime data dtype
Milestone

Comments

@chinux23
Copy link

Hello,

I am new to pandas and thanks for this great library!

I have a data frame like this:

Gold_2012.head()

                              open  high    low close   volume
date_time                   
2012-01-02 18:01:00  1571.0  1571.0  1569.1  1569.8  351
2012-01-02 18:02:00  1569.8  1570.0  1569.7  1569.8  54
2012-01-02 18:03:00  1570.0  1570.0  1569.1  1569.9  247
2012-01-02 18:04:00  1570.0  1570.0  1569.8  1569.9  55
2012-01-02 18:05:00  1569.8  1569.9  1568.5  1568.5  48

I am trying to access the first element of this dataframe. If I use loc function, everything works out:

Gold_2012.loc[Gold_2012.index[0]]


open      1571.0
high      1571.0
low       1569.1
close     1569.8
volume     351.0
Name: 2012-01-02 18:01:00-06:00, dtype: float64

But if I do something like this, an error is thrown. Is this expected?

Gold_2012[Gold_2012.index[0]]

KeyError Traceback (most recent call last)
in ()
----> 1 Gold_2012[Gold_2012.index[0]]

/Users/chen/Virtualenvs/python3Env/lib/python3.3/site-packages/pandas/core/frame.py in getitem(self, key)
1926 else:
1927 # get column
-> 1928 return self._get_item_cache(key)
1929
1930 def _getitem_slice(self, key):

/Users/chen/Virtualenvs/python3Env/lib/python3.3/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
568 return cache[item]
569 except Exception:
--> 570 values = self._data.get(item)
571 res = self._box_item_values(item, values)
572 cache[item] = res

/Users/chen/Virtualenvs/python3Env/lib/python3.3/site-packages/pandas/core/internals.py in get(self, item)
1382
1383 def get(self, item):
-> 1384 _, block = self._find_block(item)
1385 return block.get(item)
1386

/Users/chen/Virtualenvs/python3Env/lib/python3.3/site-packages/pandas/core/internals.py in _find_block(self, item)
1524
1525 def _find_block(self, item):
-> 1526 self._check_have(item)
1527 for i, block in enumerate(self.blocks):
1528 if item in block:

/Users/chen/Virtualenvs/python3Env/lib/python3.3/site-packages/pandas/core/internals.py in _check_have(self, item)
1531 def _check_have(self, item):
1532 if item not in self.items:
-> 1533 raise KeyError('no item named %s' % com.pprint_thing(item))
1534
1535 def reindex_axis(self, new_axis, method=None, axis=0, copy=True):

KeyError: 'no item named 2012-01-02 18:01:00-06:00'

@jreback
Copy link
Contributor

jreback commented Jun 17, 2013

@chinux23 .loc is the correct method of access here, as df[...] is really columnar access; though we do provide row slicing if possible
e.g.

df['2012-01-02 18:01'] would have worked , however using a datetime object was not supported

see associated PR #3925 which implementes this

@jreback
Copy link
Contributor

jreback commented Jun 17, 2013

@chinux23 changing this interface makes things inconsistent, so use .loc

@jreback
Copy link
Contributor

jreback commented Jun 17, 2013

closing this a not a bug

@jreback jreback closed this as completed Jun 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype
Projects
None yet
Development

No branches or pull requests

2 participants