关于pandas_datareader读取yahoo数据问题

一、环境

    window7、Python3.6

二、问题

    在《利用Python进行数据分析》一书中,学习到pandas这一章节,利用书中的方法去获取雅虎数据会出现以下问题:

    问题一

import pandas.io.data as web
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pandas.io.data as web
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\io\data.py", line 2, in <module>
    "The pandas.io.data module is moved to a separate package "
ImportError: The pandas.io.data module is moved to a separate package (pandas-datareader).
 After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), 
you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader import data, wb``.
        根据提示,我们很快可以知道,‘pandas.io’这个模块已经变成了‘pandas_datareader’了,cmd命令行中执行以下语句即可解决这个问题:
pip install pandas_datareader

用以下语句可以调用模块

import pandas_datareader.data as web

当你觉得一切都准备就绪的时候,第二个问题又来了(如果你跟我一样不幸的话)。

初步认为是由于雅虎受中国市场限制所导致的,接下来讲解解决方法。

    问题二:

web.get_data_yahoo('AAPL','1/1/2000','1/1/2010')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    web.get_data_yahoo('AAPL','1/1/2000','1/1/2010')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas_datareader\data.py", line 63, in get_data_yahoo
    raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Actions'))
pandas_datareader.exceptions.ImmediateDeprecationError: 
Yahoo Actions has been immediately deprecated due to large breaks in the API without the

introduction of a stable replacement. Pull Requests to re-enable these data connectors are welcome.

See https://github.com/pydata/pandas-datareader/issues

    在网上搜了好久才找到这一方法,这里我们需要引入另外一个模块‘fix_yahoo_finance’,同样使用pip方法进行安装

pip install fix_yahoo_finance

    调用方式如下:

import pandas_datareader.data as web
import datetime
import fix_yahoo_finance as yf
yf.pdr_override()

start=datetime.datetime(2006, 10, 1)
end=datetime.datetime(2012, 1, 1)
web.get_data_yahoo('AAPL',start,end)
    这样子就可以下载雅虎数据了。




猜你喜欢

转载自blog.csdn.net/Smile_Smilling/article/details/79934900