4

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...

2
  • What do you want to do with these times, ultimately? Help us understand where you're going with this, and you'll get a clearer answer. Commented Apr 3, 2013 at 15:54
  • I want to subtract subtract stockTime[1] - stockTime[0] so I need the value of the first and the second element... ultimately i'm trying to build a candlestickchart and want to have the right widths for the bars Commented Apr 3, 2013 at 18:00

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

hm.... but my question is with regards to how I get the Timestamp part of the stock out? I have no idea how to access it other than what I showed..
@evan54 it's not entirely clear on what the structure is of your DataFrame, an example would help a lot, e.g. the output of df.head(). Certainly you want to avoid using a for loop.
the output I expect is the same as from the code I've posted. I updated the question to include .head output and the expected output of the function I'm after... Hopefully that clears things up
Still not sure what you're asking, but if you have a function for one value, you can then use Series/DataFrame apply or index's map, e.g. df.index.map(lambda t: t.value) ?
3

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).

Comments

2

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

Comments

0

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

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

3 Comments

any way of accessing it out of a loop? I mean this is pretty much what I showed right? I want to get stock['Time'] or something like that..
What output do you expect?
I posted what I expect, it is basically what the .next implementation outputs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.