Akavall
Akavall

Reputation: 86138

Extract date from Pandas DataFrame

I want to download adjusted close prices and their corresponding dates from yahoo, but I can't seem to figure out how to get dates from pandas DataFrame.

I was reading an answer to this question

from pandas.io.data import DataReader
from datetime import datetime
goog = DataReader("GOOG",  "yahoo", datetime(2000,1,1), datetime(2012,1,1))
print goog["Adj Close"]

and this part works fine; however, I need to extract the dates that correspond to the prices.

For example:

adj_close = np.array(goog["Adj Close"])

Gives me a 1-D array of adjusted closing prices, I am looking for 1-D array of dates, such that:

date = # what do I do?

adj_close[0] corresponds to date[0]

When I do:

>>> goog.keys()
Index([Open, High, Low, Close, Volume, Adj Close], dtype=object)

I see that none of the keys will give me anything similar to the date, but I think there has to be a way to create an array of dates. What am I missing?

Upvotes: 2

Views: 8408

Answers (2)

Gaspare Bonventre
Gaspare Bonventre

Reputation: 1144

import numpy as np
import pandas as pd
from pandas.io.data import DataReader

symbols_list = ['GOOG','IBM']
d = {}
for ticker in symbols_list:
    d[ticker] = DataReader(ticker, "yahoo", '2014-01-01')
pan = pd.Panel(d)
df_adj_close = pan.minor_xs('Adj Close') #also use 'Open','High','Low','Adj Close' and 'Volume'

#the dates of the adjusted closes from the dataframe containing adjusted closes on multiple stocks
df_adj_close.index

# create a dataframe that has data on only one stock symbol
df_individual = pan.get('GOOG')

# the dates from the dataframe of just 'GOOG' data
df_individual.index

Upvotes: 1

waitingkuo
waitingkuo

Reputation: 93754

You can get it by goog.index which is stored as a DateTimeIndex. To get a series of date, you can do

goog.reset_index()['Date']

Upvotes: 4

Related Questions