Pandas datareader错误

时间:2017-12-01 06:17:43

标签: python pandas yahoo datareader

我使用谷歌作为数据提供者已成功使用这样的脚本数月。几天前它停止了工作。这是输入:

import numpy as np
import pandas as pd
import pandas_datareader.data as wb
from datetime import date, datetime, time, timedelta
from pandas.tseries.offsets import BDay

data_provider = 'google' # 'google', 'quandl', or 'yahoo'

today = date.today()
four_days_ago = today - BDay (4)

tickers = [{'ACET':1},{'HBAN':2},{'HPQ':3},{'IPG':4},{'AOSL':5},{'HBI':6},{'NRG':7},{'IBM':8},{'MMM':9}]

enter_list = []
rank_list = []

for x in tickers:
    for ticker, rank in x.items():
        try:
            df = wb.DataReader (ticker, data_provider, four_days_ago, today)
            print ('reading data for ' + ticker + ' from ' + data_provider)
        except:
            print ('\ncan not read data for ' + ticker + ' from ' + data_provider)

        if df.iloc[1]['Low'] < df.iloc[0]['Low']:
            enter_list.append (ticker)
            rank_list.append (rank)

df2 = pd.DataFrame({'ticker' : enter_list, 'rank' : rank_list}, columns = ['ticker', 'rank'])

if len(df2) == 0:
    print ('\nthere are no qualifying stocks')
else:
    print ('\nlower stocks\n')
    print (df2)

这是输出:

reading data for ACET from google
Traceback (most recent call last):
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site-packages\pandas\core\indexes\base.py", line 2483, in get_value
return libts.get_value_box(s, key)
  File "pandas\_libs\tslib.pyx", line 923, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18843)
  File "pandas\_libs\tslib.pyx", line 932, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18477)
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\stocks\question 8 for stack overflow.py", line 25, in <module>
if df.iloc[1]['Low'] < df.iloc[0]['Low']:
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site-packages\pandas\core\series.py", line 601, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site-packages\pandas\core\indexes\base.py", line 2491, in get_value
raise e1
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site-packages\pandas\core\indexes\base.py", line 2477, in get_value
tz=getattr(series.dtype, 'tz', None))
  File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4404)
  File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4087)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: 'Low'

以下是数据提供程序为quandl时的输出:

reading data for ACET from quandl
reading data for HBAN from quandl
reading data for HPQ from quandl
reading data for IPG from quandl
reading data for AOSL from quandl

can not read data for HBI from quandl
reading data for NRG from quandl
reading data for IBM from quandl
reading data for MMM from quandl

lower stocks

  ticker  rank
0   HBAN     2
1    IPG     4
2    IBM     8
3    MMM     9

以下是数据阅读器为yahoo时的输出:

reading data for ACET from yahoo
reading data for HBAN from yahoo
reading data for HPQ from yahoo
reading data for IPG from yahoo
reading data for AOSL from yahoo
reading data for HBI from yahoo
reading data for NRG from yahoo

can not read data for IBM from yahoo
reading data for MMM from yahoo

lower stocks

  ticker  rank
0   HBAN     2
1    HPQ     3
2    IPG     4
3   AOSL     5
4    HBI     6
5    NRG     7
6    IBM     8

yahoo输出在运行时会发生变化。以下是一些后来的输出:

reading data for ACET from yahoo
reading data for HBAN from yahoo
reading data for HPQ from yahoo
reading data for IPG from yahoo
reading data for AOSL from yahoo
reading data for HBI from yahoo
reading data for NRG from yahoo
reading data for IBM from yahoo
reading data for MMM from yahoo

lower stocks

  ticker  rank
0   HBAN     2
1    HPQ     3
2    IPG     4
3   AOSL     5
4    HBI     6
5    NRG     7

有谁知道这里发生了什么? 问题是由于使用循环创建多个数据帧吗? 问题是服务器ping太多/太快了吗?

提前致谢, 大卫

0 个答案:

没有答案