python - Pandas 数据读取器引发 AttributeError : module 'pandas.io' has no attribute 'data'

标签 python pandas

这是我正在尝试的代码

import matplotlib.pyplot as plt    
import pandas as pd
ticker = 'GLD'
begdate = '2014-11-11'
enddate = '2016-11-11'
data1 = pd.io.data.DataReader(ticker,'yahoo',dt.datetime(2014,11,11),dt.datetime(2016,11,11))
gld_df = pd.DataFrame(data1)
date_df = pd.to_datetime(list(gld_df.index))
adj_close_df = list(gld_df["Adj Close"])
plt.plot(date_df,adj_close_df)
plt.title("SPDR Gold Shares ")

它给我以下错误。几天前,当我尝试相同的代码时没有出现错误。

runfile('D:/Quant/MSQF/4 - Algorithms 1/3-Sorting/Mini Project 2_v2.py', wdir='D:/Quant/MSQF/4 - Algorithms 1/3-Sorting')
Traceback (most recent call last):

  File "<ipython-input-10-db75eb5622f8>", line 1, in <module>
    runfile('D:/Quant/MSQF/4 - Algorithms 1/3-Sorting/Mini Project 2_v2.py', wdir='D:/Quant/MSQF/4 - Algorithms 1/3-Sorting')

  File "D:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Quant/MSQF/4 - Algorithms 1/3-Sorting/Mini Project 2_v2.py", line 18, in <module>
    data1 = pd.io.data.DataReader(ticker,'yahoo',dt.datetime(2014,11,11),dt.datetime(2016,11,11))

AttributeError: module 'pandas.io' has no attribute 'data'

我正在使用 Anaconda、Python 3.x。这是 Pandas 的问题还是我的系统有问题?

最佳答案

pandas 已删除该功能,现在作为不同的包提供 (link):

DataReader The sub-package pandas.io.data is removed in favor of a separately installable pandas-datareader package. This will allow the data modules to be independently updated to your pandas installation. The API for pandas-datareader v0.1.1 is the same as in pandas v0.16.1. (GH8961)

You should replace the imports of the following:

from pandas.io import data, wb

With the following:

from pandas_datareader import data, wb

安装 pandas_datareader使用 pip install pandas-datareader 并将代码替换为以下内容:

from pandas_datareader import data
import datetime as dt
ticker = 'GLD'
begdate = '2014-11-11'
enddate = '2016-11-11'
data1 = data.DataReader(ticker,'yahoo',dt.datetime(2014,11,11),dt.datetime(2016,11,11))

关于python - Pandas 数据读取器引发 AttributeError : module 'pandas.io' has no attribute 'data' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40816169/

相关文章:

python - 在 pandas 数据框中按组进行回归,并添加包含预测值和 beta/t-stats 的列

python - 尝试在 python 中一次搜索 2 个字符

python - Tkinter 为每个单选按钮返回相同的值?

python - 如何从 pandas 的列表中向 Dataframe 添加行?

python - 在 Python 中使用列名构建 DataFrame

python - 列表元素的计数器

python - 如果pandas中的值是 float (不是字符串),则将列除以其他列?

python - 如何通过更改另一个属性的值来更改一个属性的值? (依赖属性)

java - 在 IMAP 帐户中,如何创建仅包含交换附件的邮件的精确副本?

python - Json,抓取到网页 - python