nick appel
nick appel

Reputation: 302

Pandas yahoo finance DataReader

I am trying to get the Adj Close prices from Yahoo Finance into a DataFrame. I have all the stocks I want but I am not able to sort on date.

stocks = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
ls_key = 'Adj Close'
start = datetime(2014,1,1)
end = datetime(2014,3,28)    
f = web.DataReader(stocks, 'yahoo',start,end)


cleanData = f.ix[ls_key]
dataFrame = pd.DataFrame(cleanData)

print dataFrame[:5]

I get the following result, which is almost perfect.

              IBM   MSFT   ORCL    TSLA   YELP
Date                                           
2014-01-02  184.52  36.88  37.61  150.10  67.92
2014-01-03  185.62  36.64  37.51  149.56  67.66
2014-01-06  184.99  35.86  37.36  147.00  71.72
2014-01-07  188.68  36.14  37.74  149.36  72.66
2014-01-08  186.95  35.49  37.61  151.28  78.42

However, the Date is not an Item. so when I run:

print dataFrame['Date']

I get the error:

KeyError: u'no item named Date'

Hope anyone can help me adding the Date.

Upvotes: 19

Views: 73888

Answers (8)

noobninja
noobninja

Reputation: 910

import pandas_datareader.data as web
import datetime
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2016, 1, 1)
web.DataReader('GOOGL', 'yahoo', start, end)

Upvotes: 0

Vibhutha Kumarage
Vibhutha Kumarage

Reputation: 1399

The sub-package pandas.io.data is removed from the latest pandas package and it is available to install separately as pandas-datareader

use git to install the package. in the linux terminal:

git clone https://github.com/pydata/pandas-datareader.git
cd pandas-datareader
python setup.py install

now you can use import pandas_datareader to your python script for Remote data Access.

For more information Use this link to visit the latest documentation

Upvotes: 1

skysign
skysign

Reputation: 1124

print(dataFrame.index[0])

2014-01-02 00:00:00

Upvotes: 0

J-Eubanks
J-Eubanks

Reputation: 381

Date is in the index values.

To get it into a column value, you should just use:

dataframe.reset_index(inplace=True,drop=False)

Then you can use

dataframe['Date'] 

because "Date" will now be one of the keys in your columns of the dataframe.

Upvotes: 7

Dave Paper
Dave Paper

Reputation: 111

import pandas_datareader.data as web
import datetime    

start = datetime.datetime(2013, 1, 1)
end = datetime.datetime(2016, 1, 27)
df = web.DataReader("GOOGL", 'yahoo', start, end)

dates =[]
for x in range(len(df)):
    newdate = str(df.index[x])
    newdate = newdate[0:10]
    dates.append(newdate)

df['dates'] = dates

print df.head()
print df.tail()

Upvotes: 11

user3772084
user3772084

Reputation: 31

Use dataFrame.index to directly access date or to add an explicit column, use dataFrame["Date"] = dataframe.index

stocks = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
ls_key = 'Adj Close'
start = datetime(2014,1,1)
end = datetime(2014,3,28)    
f = web.DataReader(stocks, 'yahoo',start,end)


cleanData = f.ix[ls_key]
dataFrame = pd.DataFrame(cleanData)
dataFrame["Date"] = dataframe.index
print dataFrame["Date"] ## or print dataFrame.index

Upvotes: 3

Femto Trader
Femto Trader

Reputation: 2014

f is a Panel You can get a DataFrame and reset index (Date) using:

f.loc['Adj Close',:,:].reset_index()

but I'm not sure reset_index() is very useful as you can get Date using

f.loc['Adj Close',:,:].index

You might have a look at http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing about indexing

Upvotes: 0

Gaspare Bonventre
Gaspare Bonventre

Reputation: 1154

This should do it.

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

symbols_list = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
d = {}
for ticker in symbols_list:
    d[ticker] = DataReader(ticker, "yahoo", '2014-12-01')
pan = pd.Panel(d)
df1 = pan.minor_xs('Adj Close')
print(df1)

#df_percent_chg = df1.pct_change()

Upvotes: 2

Related Questions