使用 Pandas DataReader 获取“Adj Close”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38133064/
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
Get "Adj Close" using pandas DataReader
提问by Evy555
I just switched from pandas.io to pandas_datareader and I'm having difficulties pulling in just Adjusted Close prices. before I could use the following code
我刚刚从pandas.io 切换到pandas_datareader,但在获取调整后的收盘价时遇到了困难。在我可以使用以下代码之前
pd.io.data.get_data_yahoo(stock, start, end)['Adj Close']
now when I try the datareader (imported as web) it does not work.
现在,当我尝试使用数据读取器(作为 Web 导入)时,它不起作用。
web.get_data_yahoo(stock, start, end)['Adj Close']
I've tried to find documentation to see if there is a new argument that the pandas_datareader uses, but I have had no luck. Is there anyway to pull in just Adjusted Close data using the new pandas library?
我试图找到文档以查看是否有 pandas_datareader 使用的新参数,但我没有运气。无论如何,是否可以使用新的 Pandas 库提取调整后的关闭数据?
回答by MaxU
i would use DataReaderfor that:
我会为此使用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
actually your code works as well (pandas 0.18.1 and pandas_datareader 0.2.1):
实际上,您的代码也能正常工作(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
回答by Anthony
This solution is no longer viable. When I run:
此解决方案不再可行。当我运行时:
import pandas_datareader.data as web
web.get_data_yahoo('AAPL')
This yields:
这产生:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): Max retries exceeded with url: /table.csv?a=0&ignore=.csv&s=AAPL&b=1&e=7&d=6&g=d&f=2017&c=2010 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
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 提供,或未知',))
Looks like Quanld could provide a better solution
看起来 Quanld 可以提供更好的解决方案
回答by Trent Larson
Reading from Yahoo is broken. Use Google instead, if you can. For example:
从雅虎读取已损坏。如果可以,请改用 Google。例如:
df = web.DataReader("AAPL", 'google', start, end)
回答by James Wang
Try this:
尝试这个:
from pandas_datareader import get_data_yahoo as gy
从 pandas_datareader 导入 get_data_yahoo 作为 gy
X = gy('AAPL','2019-01-01','2019-03-15')['Adj Close']
print(X.head())