pythonindex-error

IndexError: Replacement index 1 out of range for positional args tuple


I am following a tutorial and I don't know why I got this error:

    <ipython-input-61-d59f7a5a07ab> in extract_featuresets(ticker)
      2     tickers, df = process_data_for_labels(ticker)
      3     df['{}_target'.format(ticker)] = list(map(buy_sell_hold,
----> 4                                              df['{}_{}1d'.format(ticker)],
      5                                              df['{}_{}2d'.format(ticker)],
      6                                              df['{}_{}3d'.format(ticker)],

IndexError: Replacement index 1 out of range for positional args tuple

Here is my code:

tickers, df = process_data_for_labels(ticker)
df['{}_target'.format(ticker)] = list(map(buy_sell_hold,
                                         df['{}_{}1d'.format(ticker)],
                                         df['{}_{}2d'.format(ticker)],
                                         df['{}_{}3d'.format(ticker)],
                                         df['{}_{}4d'.format(ticker)],
                                         df['{}_{}5d'.format(ticker)],
                                         df['{}_{}6d'.format(ticker)],
                                         df['{}_{}7d'.format(ticker)],))

Here is the link to the tutorial: https://www.youtube.com/watch?v=zPp80YM2v7k


Solution

  • Your format string need two arguments in format while you are only passing in one ticker as argument.

    If ticker is a two element list or tuple, you can do this:

    df['{}_{}1d'.format(*ticker)]
    

    Otherwise remove one curly brackets:

    df['{}_1d'.format(ticker)]