14

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

balexander1
  • 182
  • 1
  • 1
  • 8
  • 1
    `ticker` must be a two-element tuple because there are two placeholders in the format string. Is it? – DYZ Aug 30 '20 at 07:44
  • you should be calling `tickers` instead of `ticker`... notice the previous line only unpacks one element `'{}_target'.format(ticker)` – RichieV Aug 30 '20 at 07:45

2 Answers2

27

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)]
fusion
  • 1,327
  • 6
  • 12
  • 4
    In Python `*` is used to unpack iterables (see [What is the Pythonic way to unpack tuples?](https://stackoverflow.com/questions/2238355/what-is-the-pythonic-way-to-unpack-tuples)) – Aniket Deshpande Feb 19 '21 at 08:13
6

If you want same value in { } of

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

Try like it this:

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

I solved my problem similar your problem.

Ice Bear
  • 2,676
  • 1
  • 8
  • 24
LeeYongBlood
  • 61
  • 1
  • 1