"修复雅虎财务"返回空数据帧

时间:2018-06-08 03:27:33

标签: dataframe finance yahoo

这一直在运作"好的"好几个月了。当我说"好的"时,我的意思是该程序有时会在第一次正确运行,有时需要多次尝试。在过去的几天里,它根本无法完成。其他一些帖子谈到实施"暂停"选项和" retry_count"通话中的选项。正如你在下面看到的那样,我为那些人尝试了一些荒谬的选择,但它仍然没有用。其他帖子谈到让程序进入睡眠状态。我也是长时间尝试过的,但它仍然没有用。在这样的循环中创建数据帧是否有问题?我怎样才能解决这个问题? (注意,如果您尝试下面的代码,它可能会正常运行一次,两次或x次,但最终会像下面那样崩溃)。

import numpy as np
import pandas as pd
import pandas_datareader.data as wb
import fix_yahoo_finance as yf

from datetime import date, datetime, timedelta, time
from pandas.tseries.offsets import BDay

today = date.today() 
days_ago_24 = today - BDay(24)

tick_AAXN = ['AAXN']
tick_AB =   ['AB']
tick_ACHN = ['ACHN']
tick_ACIW = ['ACIW']
tick_ACXM = ['ACXM']
tick_ADTN = ['ADTN']
tick_AEGN = ['AEGN']
tick_AEO =  ['AEO']
tick_AES =  ['AES']
tick_AFGE = ['AFGE']
tick_AFSI = ['AFSI']
tick_AGNC = ['AGNC']
tick_AJRD = ['AJRD']
tick_AKR =  ['AKR']
tick_AKRX = ['AKRX']
tick_ALLY = ['ALLY']
tick_AM =   ['AM']
tick_AMAG = ['AMAG']
tick_AMD =  ['AMD']
tick_AMH =  ['AMH']
tick_AMKR = ['AMKR']
tick_AMSWA = ['AMSWA']
tick_ANCX = ['ANCX']
tick_ANDE = ['ANDE']
tick_ANGI = ['ANGI']
tick_ANGO = ['ANGO']
tick_AOBC = ['AOBC']
tick_AOSL = ['AOSL']
tick_APLE = ['APLE']
tick_AR =   ['AR']
tick_ARAY = ['ARAY']
tick_ARCC = ['ARCC']
tick_ARES = ['ARES'] 
tick_ARLP = ['ARLP']
tick_ARNA = ['ARNA']
tick_ARNC = ['ARNC']
tick_ARRS = ['ARRS']
tick_ARRY = ['ARRY']
tick_ASB =  ['ASB'] 
tick_ATRC = ['ATRC']
tick_ATSG = ['ATSG']
tick_ATUS = ['ATUS']
tick_AVX =  ['AVX']
tick_AXDX = ['AXDX']
tick_AXGN = ['AXGN']
tick_AXTI = ['AXTI']


tick_list = [tick_AAXN, tick_AB, tick_ACHN, tick_ACIW, tick_ACXM, tick_ADTN, 
tick_AEGN, tick_AEO, tick_AES, tick_AFGE, tick_AFSI, tick_AGNC, tick_AJRD, 
tick_AKR, tick_AKRX, tick_ALLY, tick_AM,  tick_AMAG, tick_AMD, tick_AMH,  
tick_AMKR, tick_AMSWA, tick_ANCX, tick_ANDE, tick_ANGI, tick_ANGO, 
tick_AOBC, tick_AOSL, tick_APLE, tick_AR, tick_ARAY, tick_ARCC, tick_ARES, 
tick_ARLP, tick_ARNA, tick_ARNC, tick_ARRS, tick_ARRY, tick_ASB, tick_ATRC, 
tick_ATSG, tick_ATUS, tick_AVX, tick_AXDX, tick_AXGN, tick_AXTI]

enter_ticker_list = []
previous_close_list = []

def process_ticker ():
    enter_ticker_list.append (ticker)
    previous_close_list.append (df.iloc[4]['Close'])

for tick in tick_list:
    ticker = tick[0] # GE
    print (tick)
    try:
        df = yf.download(ticker, start = days_ago_24, end = today, progress = False) #, retry_count = 50, pause = 3)
        df = df.tail(5) # cut down the df to only be for the last five days
    except:
        print ('\ncan not read data for ' + ticker)
    process_ticker()
    #time.sleep(2) # pause the script so that every symbol reads correctly
results_df = pd.DataFrame({'tck' : enter_ticker_list, 'prcl' : 
previous_close_list}, columns = ['tck', 'prcl'])
print (results_df.round(2))

输出:

['AAXN']
['AB']
Traceback (most recent call last):
  File "C:\stocks\question 9 for stack overflow.py", line 77, in <module>
    process_ticker()
  File "C:\stocks\question 9 for stack overflow.py", line 67, in 
    process_ticker
    previous_close_list.append (df.iloc[4]['Close'])
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site- 
packages\pandas\core\indexing.py", line 1328, in __getitem__
    return self._getitem_axis(key, axis=0)
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site- 
packages\pandas\core\indexing.py", line 1749, in _getitem_axis
    self._is_valid_integer(key, axis)
  File "C:\Users\dvdad\Anaconda3.4.2.0.64.bit\lib\site- 
packages\pandas\core\indexing.py", line 1638, in _is_valid_integer
    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

>>> print (df)
Empty DataFrame
Columns: []
Index: []

0 个答案:

没有答案