【发布时间】:2018-07-31 10:04:23
【问题描述】:
我正在尝试使用 Pandas 和 Pandas Datareader 从 Google 财经中提取数据。 这是我的代码:
#Importing libraries needed for pulls from Google
from pandas_datareader import data
import pandas as pd
import datetime
from datetime import date
#Define the instruments to download. In this case: Apple, Microsoft, and
the S&P500 index
tickers = ['APPL', 'MSFT', 'SPY']
start_date = datetime.datetime(2017, 12, 1)
end_date = datetime.datetime(2017, 12, 31)
#Use pandas_reader.data.DataReader to load the desired data
panel_data = data.DataReader('SPY', 'google', start_date, end_date)
#Getting just the adjusted closing prices. This will return a Pandas DataFrame
#The index in this DataFrame is the major index of the panel_data.
close = panel_data.ix['Close']
#Getting all weekdays within date range.
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')
#How do we align the existing prices in the adj_close with out new set of dates?
#All we need to do is reindex close using all_weekdays as the new index.
close = close.reindex(all_weekdays)
close.head(10)
这是控制台输出:
runfile('C:/Users/kjohn_000/.spyder-py3/temp.py', wdir='C:/Users/kjohn_000/.spyder-py3')
C:\Users\kjohn_000\Anaconda3\lib\site-packages\pandas_datareader\base.py:201: SymbolWarning: Failed to read symbol:
'APPL', replacing with NaN.
warnings.warn(msg.format(sym), SymbolWarning)
C:\Users\kjohn_000\Anaconda3\lib\site-
packages\pandas_datareader\base.py:201: SymbolWarning: Failed to read
symbol: 'MSFT', replacing with NaN.
warnings.warn(msg.format(sym), SymbolWarning)
C:\Users\kjohn_000\Anaconda3\lib\site-packages\pandas_datareader\base.py:201: SymbolWarning: Failed to read symbol:
'SPY', replacing with NaN.
warnings.warn(msg.format(sym), SymbolWarning)
Traceback (most recent call last):
File "<ipython-input-2-0ddd75de0396>", line 1, in <module>
runfile('C:/Users/kjohn_000/.spyder-py3/temp.py',
wdir='C:/Users/kjohn_000/.spyder-py3')
File "C:\Users\kjohn_000\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\kjohn_000\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/kjohn_000/.spyder-py3/temp.py", line 14, in <module>
panel_data = data.DataReader(tickers, dataSource, start_date, end_date)
File "C:\Users\kjohn_000\Anaconda3\lib\site-packages\pandas_datareader\data.py", line 137, in DataReader
session=session).read()
File "C:\Users\kjohn_000\Anaconda3\lib\site-
packages\pandas_datareader\base.py", line 186, in read
df = self._dl_mult_symbols(self.symbols)
File "C:\Users\kjohn_000\Anaconda3\lib\site-
packages\pandas_datareader\base.py", line 206, in _dl_mult_symbols
raise RemoteDataError(msg.format(self.__class__.__name__))
RemoteDataError: No data fetched using 'GoogleDailyReader'
为什么 Pandas Datareader 无法读取“股票代码”列表中的股票代码?我已经四处寻找答案几个小时了,但是许多答案都是关于雅虎 API 的回答问题,其余的答案要么是针对另一种语言,要么只是在编码过程中超出了我的深度(我相对Python 新手)。提前感谢您的帮助和反馈。
【问题讨论】:
-
This github issue 是最近与雅虎无关的问题。也许它在谷歌重组他们的 API 期间正在进行?或者您可能需要更新 pandas。
-
我明白了。非常有用的链接。据 Anaconda 所知,我的 Pandas 版本是最新的,但它尝试调用的 url 是“google.com/finance/historical”而不是“finance.google.com/finance/historical”。后一个 url 在 API 重组时是正确的。 Anaconda 用户要更改的文件的路径是 Anaconda3\Lib\site-packages\pandas_datareader\google\。感谢您的帮助!
标签: python python-3.x pandas google-finance pandas-datareader