evan54
evan54

Reputation: 3733

how to access timestamp in DataFrame pandas?

Having downloaded data from yahoo for a stock using the get_data_yahoo I then want to access the time for each row... How do I do that?

One way I've kind of figured out to do this is:

it = stock.iterrows()
st0 = it.next()
resultIWant = st0[0].value # this gives what I want (almost)
print resultIWant

EDIT:

So basically I want something like stock['Open'] but for time, smth like stock['Time'] would be ideal..

some one asked for the output of .head

            Open  High   Low  Close  Volume  Adj Close
Date                                                  
2012-04-03  2.06  2.09  2.01   2.08  463200       2.08
2012-04-04  2.04  2.05  2.01   2.02  335600       2.02

Expected output from function:

print find_time(stock,2) # function I'm looking for
1333497600000000000      # resulting output

The expected output is the time from the last epoch for each of the dates in an array or some other way to get the time of each entry. The example code I gave gives me the results I want however if I want to get access to the 4th element the only way to do it would be to .next the iterator 4 times, this seems like a bad method.

First Question: Is there a more obvious way, what I'm doing doesn't seem like the best way to do this.

Second Question: What units is the result in? I think it's nanoseconds but not sure...

Upvotes: 4

Views: 10313

Answers (4)

MasterKZ
MasterKZ

Reputation: 31

Just seen the question. But may be it is interesting to somebody.

In this case you can make next command:

stock['Time'] = stock.index 

then you'll get new column 'Time' cause your Date is the index. To convert the DateTime to Unix Time change to:

import numpy as np

stock['Time'] = stock.index.astype(np.int64).

Upvotes: 3

evan54
evan54

Reputation: 3733

The correct answer I found in the end here

You can access the rows using st.ix[index_val] from section 5.2.8 of the reference.

ts = st.ix[2].Name

which returns a timestamp

ts is the same as the st[0] in the question

Upvotes: 2

waitingkuo
waitingkuo

Reputation: 93754

In fact each row contains a tuple contains index and data, so you might do this:

for index, row in stock.iterrows():
    print index

Upvotes: 0

Andy Hayden
Andy Hayden

Reputation: 375377

Timestamps have a time method:

In [1]: t = pd.Timestamp('200101011300')

In [2]: t
Out[2]: <Timestamp: 2001-01-01 13:00:00>

In [3]: t.time()
Out[3]: datetime.time(13, 0)

The value is nanoseconds since midnight 1 January 1970 i.e. unix time * 10 ** 9:

In [4]: t.value
Out[4]: 978354000000000000

Upvotes: 3

Related Questions