使用pandas DataReader获取“Adj Close”

问题描述 投票:2回答:4

我刚刚从pandas.io切换到pandas_datareader,我很难将调整后的收盘价拉进去。在我可以使用以下代码之前

pd.io.data.get_data_yahoo(stock, start, end)['Adj Close']

现在当我尝试使用datareader(导入为web)时,它不起作用。

web.get_data_yahoo(stock, start, end)['Adj Close'] 

我试图找到文档,看看pandas_datareader是否有一个新参数,但我没有运气。无论如何使用新的pandas库来调整调整后的关闭数据?

python pandas datareader yahoo-finance
4个回答
3
投票

我会用DataReader

In [61]: from pandas_datareader.data import DataReader

In [62]: DataReader('AAPL', 'yahoo', '2016-06-25', '2016-06-30')['Adj Close']
Out[62]:
Date
2016-06-27    92.040001
2016-06-28    93.589996
2016-06-29    94.400002
Name: Adj Close, dtype: float64

实际上你的代码也可以运行(pandas 0.18.1和pandas_datareader 0.2.1):

In [63]: import pandas_datareader.data as web

In [64]: web.get_data_yahoo('AAPL', '2016-06-25', '2016-06-30')
Out[64]:
                 Open       High        Low      Close    Volume  Adj Close
Date
2016-06-27  93.000000  93.050003  91.500000  92.040001  45489600  92.040001
2016-06-28  92.900002  93.660004  92.139999  93.589996  39311500  93.589996
2016-06-29  93.970001  94.550003  93.629997  94.400002  36427800  94.400002

In [65]: web.get_data_yahoo('AAPL', '2016-06-25', '2016-06-30')['Adj Close']
Out[65]:
Date
2016-06-27    92.040001
2016-06-28    93.589996
2016-06-29    94.400002
Name: Adj Close, dtype: float64

0
投票

这种解决方案不再可行。当我跑:

import pandas_datareader.data as web
web.get_data_yahoo('AAPL')

这会产生:

requests.exceptions.ConnectionError:HTTPConnectionPool(host ='ichart.finance.yahoo.com',port = 80):使用url超出最大重试次数:/table.csv?a = 0 &ignore = .csv&s = AAPL&b = 1&e = 7&d = 6&g = d&f = 2017&c = 2010(由NewConnectionError引起(':无法建立新连接:[Errno 8]提供nodename或servname,或者未知',))

看起来Quanld可以提供更好的solution


0
投票

雅虎的阅读被打破了。如果可以,请改用Google。例如:

df = web.DataReader("AAPL", 'google', start, end)

0
投票

试试这个:

来自pandas_datareader导入get_data_yahoo作为gy

X = gy('AAPL','2019-01-01','2019-03-15')['Adj Close']

print(X.head())
© www.soinside.com 2019 - 2024. All rights reserved.