使用 pandas web.DataReader 从整个索引(例如 DJIA)中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23283111/
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
Grabbing data from entire index (e.g., DJIA) using pandas web.DataReader
提问by user3314418
I know how to get individual stocks. How might I get data for an entire index, like the DJI? https://www.google.com/finance?q=INDEXDJX%3A.DJI&ei=zsVZU4iADYKI6AGoXA
我知道如何获得个股。我如何获取整个索引的数据,例如 DJI? https://www.google.com/finance?q=INDEXDJX%3A.DJI&ei=zsVZU4iADYKI6AGoXA
I'd like to analyze the stock market as a whole from as far back as possible
我想从尽可能远的地方分析整个股市
start, end = dt.datetime(1950, 1, 1), dt.datetime(2013, 12, 31)
data = web.DataReader('.DJI', 'yahoo', start, end)
回答by Jacob Amos
Google Finance and Yahoo Finance handle their symbols for indices differently. Google would denote the Dow as ".DJI" whereas in Yahoo it would be "^DJI".
谷歌财经和雅虎财经以不同的方式处理它们的指数符号。谷歌将道琼斯指数表示为“.DJI”,而在雅虎中则为“^DJI”。
For some reason when I run the code Pandas is having trouble finding data for the Dow from Yahoo, but it can find it for the S&P and the Nasdaq.
出于某种原因,当我运行代码时,Pandas 无法从雅虎找到道琼斯指数的数据,但它可以找到标准普尔和纳斯达克指数的数据。
# this works
web.DataReader('^GSPC','yahoo') # S&P 500
web.DataReader('^IXIC','yahoo') # NASDAQ
# this doesn't
web.DataReader('^DJI','yahoo') # Dow
If you specifically want Dow data, Pandas also lets you use FRED data, so you can get alternatively take that route, though it won't include all the price data, just the close prices.
如果您特别需要 Dow 数据,Pandas 还允许您使用 FRED 数据,因此您可以选择采用该路线,尽管它不会包括所有价格数据,仅包括收盘价。
web.DataReader('DJIA','fred')
Another possibility would be to use Quandl. They have tons of datasets (financial, economic, demographic, etc.) that might be useful for market analysis. While it still only gets the close prices and requires knowing their sometimes cryptic "codes", here is a sample:
另一种可能性是使用Quandl。他们拥有大量可能对市场分析有用的数据集(金融、经济、人口统计等)。虽然它仍然只获得收盘价并且需要知道他们有时神秘的“代码”,但这里有一个示例:
import Quandl
dow_code = 'BCB/UDJIAD1'
Quandl.get(dow_code)
You may need to create a Quandl account (it's free) to get the authorization token that allows external mining into their database, but this is another possible workaround for you :)
您可能需要创建一个 Quandl 帐户(它是免费的)来获取允许外部挖掘到他们的数据库的授权令牌,但这是您的另一种可能的解决方法:)
Hope this helps.
希望这可以帮助。

