使用betfairlightweight时出现索引错误

时间:2019-03-29 15:20:19

标签: python pandas

我是该网站的新手,但是对Python来说还是新手,所以如果我因遇到的错误而遗漏了一些明显的东西,请原谅。 我已经开始尝试使用betfairlightweight,尝试分析来自betfair API的一些数据并将其添加到数据帧中。我创建的脚本偶尔会起作用,但通常会产生“ IndexError:列表索引超出范围”。

完整错误在这里:

Traceback (most recent call last):
  File "C:/Users/leonf/PycharmProjects/betfair_historic/test 1.py", line 120, in <module>
    runners_df = process_runner_books(market_book.runners)
  File "C:/Users/leonf/PycharmProjects/betfair_historic/test 1.py", line 23, in process_runner_books
    in runner_books]
  File "C:/Users/leonf/PycharmProjects/betfair_historic/test 1.py", line 22, in <listcomp>
    for runner_book
IndexError: list index out of range

该错误表明我正在尝试访问不存在的列表中的项目,并且我相信该错误发生在process_runner_books函数中(这实际上是我从betfair开发人员网站上获取的示例代码,我自己的代码)。 我发现有时API返回的数据不完整,并且会导致列表为空。在这一点上,我很高兴自己发现了这个问题,因此开始编写一些代码来忽略空列表:

def isEmpty(a):
    return all([isEmpty(b) for b in a]) if isinstance(a, list) else False

然后在process_runner_books函数的开头插入以下行:

if isEmpty(runner_book.ex.available_to_back) ==False:

我的想法是,如果列表不为空,则该函数的其余部分应运行;如果该列表为空,则该函数不执行任何操作。

这不起作用,并产生了相同的错误。

这是我认为正在发生错误的函数(对长度表示歉意,但我不确定如果将其截断,是否有意义):

def process_runner_books(runner_books):
    best_back_prices = [runner_book.ex.available_to_back[0].price
                        if runner_book.ex.available_to_back[0].price
                        else 1.01
                        for runner_book
                        in runner_books]
    best_back_sizes = [runner_book.ex.available_to_back[0].size
                       if runner_book.ex.available_to_back[0].size
                       else 1.01
                       for runner_book
                       in runner_books]

    best_lay_prices = [runner_book.ex.available_to_lay[0].price
                       if runner_book.ex.available_to_lay[0].price
                       else 1000.0
                       for runner_book
                       in runner_books]
    best_lay_sizes = [runner_book.ex.available_to_lay[0].size
                      if runner_book.ex.available_to_lay[0].size
                      else 1.01
                      for runner_book
                      in runner_books]

    selection_ids = [runner_book.selection_id for runner_book in runner_books]
    last_prices_traded = [runner_book.last_price_traded for runner_book in runner_books]
    total_matched = [runner_book.total_matched for runner_book in runner_books]
    statuses = [runner_book.status for runner_book in runner_books]
    scratching_datetimes = [runner_book.removal_date for runner_book in runner_books]
    adjustment_factors = [runner_book.adjustment_factor for runner_book in runner_books]

    df = pd.DataFrame({
        'Selection ID': selection_ids,
        'Best Back Price': best_back_prices,
        'Best Back Size': best_back_sizes,
        'Best Lay Price': best_lay_prices,
        'Best Lay Size': best_lay_sizes,
        'Last Price Traded': last_prices_traded,
        'Total Matched': total_matched,
        'Status': statuses,
        'Removal Date': scratching_datetimes,
        'Adjustment Factor': adjustment_factors
    })
    return df

这是使用上述功能的代码:

market_IDs = []
for market_catalogue in market_catalogues:
    market_IDs = market_IDs + [market_catalogue.market_id]
GB_runners_df = pd.DataFrame()  # create an empty dataframe to append other dfs to
# market book request
for market in market_IDs:
    # Create a price filter. Get all traded and offer data
    price_filter = filters.price_projection(
        price_data=['EX_BEST_OFFERS']
    )
    market_books = trading.betting.list_market_book(
        market_ids=[market],
        price_projection=price_filter
    )
    # append the new market book runners to the final dataframe
    market_book = market_books[0]
    runners_df = process_runner_books(market_book.runners)
    runners_df['Event_ID'] = market
    GB_runners_df = GB_runners_df.append(runners_df, ignore_index=True)

我希望获得一个数据框(GB_runners_df),其中包含有关跑步者的信息,以及带有事件ID的附加列。我偶尔会得到此结果,但是我通常会遇到上述错误。 外观示例(从一次没有错误开始)-

    Selection ID  Best Back Price  ...  Adjustment Factor     Event_ID
0          15067             3.15  ...             32.258  1.156783632
1       11985860             4.60  ...             20.281  1.156783632
2       10308561             6.40  ...             15.701  1.156783632
3       15786072             7.00  ...             14.316  1.156783632
4       11889221            11.00  ...              9.271  1.156783632
5          19453            23.00  ...              4.233  1.156783632
6        6363328            38.00  ...              2.318  1.156783632
7        7364735            46.00  ...              1.622  1.156783632
8       11487425             3.80  ...             25.974  1.156783637

1 个答案:

答案 0 :(得分:0)

此问题是由跑步者没有任何价格引起的,您需要更新价格/尺寸代码,例如:

best_back_prices = [runner_book.ex.available_to_back[0].price
                    if runner_book.ex.available_to_back
                    else 1.01
                    for runner_book
                    in runner_books]

(闲散的小组已向我提示了该问题,请随时加入,因为我们将能够更快地回答您的问题)

https://github.com/liampauling/betfair