Python Pandas 雅虎财经DataReader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22991567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:06:13  来源:igfitidea点击:

Pandas yahoo finance DataReader

pythonpandas

提问by nick appel

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.

我正在尝试将 Yahoo Finance 的 Adj Close 价格放入 DataFrame 中。我有我想要的所有股票,但我无法按日期排序。

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.

希望任何人都可以帮助我添加日期。

回答by user3772084

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

使用dataFrame.index直接访问日期或增加一个显柱,使用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

回答by Gaspare Bonventre

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

回答by Femto Trader

fis a PanelYou can get a DataFrameand reset index (Date) using:

fPanel你可以得到一个DataFrame使用和复位指数(日期):

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

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

但我不确定它reset_index()是否非常有用,因为您可以使用 Date

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

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

您可能会查看 有关索引的http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing

回答by Dave Paper

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

回答by J-Eubanks

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.

因为“日期”现在将成为数据框列中的键之一。

回答by skysign

print(dataFrame.index[0])

打印(数据帧。索引[0])

2014-01-02 00:00:00

2014-01-02 00:00:00

回答by Bodhi94

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

子包pandas.io.data从最新的pandas包中删除,可以单独安装为pandas-datareader

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

使用 git 安装包。在 linux 终端中:

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

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

现在您可以使用import pandas_datareaderPython 脚本进行远程数据访问。

For more information Use this link to visit the latest documentation

有关更多信息,请使用此链接访问最新文档

回答by noobninja

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)