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

3

18

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

Fenny answered 30/8, 2020 at 7:41 Comment(2)
ticker must be a two-element tuple because there are two placeholders in the format string. Is it?Bowlin
you should be calling tickers instead of ticker... notice the previous line only unpacks one element '{}_target'.format(ticker)Idiotic
C
31

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)]
Comedy answered 30/8, 2020 at 7:46 Comment(1)
In Python * is used to unpack iterables (see What is the Pythonic way to unpack tuples?)Parrot
O
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.

Ormolu answered 11/1, 2021 at 4:14 Comment(0)
P
1

In my situation, I was replacing values in a templated unit test python file. It contained an old string formatting command:

# bad code
self.logger.error("Test Case 1: FAILED with reason: {}".format(failure_message))

# fixed code
self.logger.error("Test Case 1: FAILED with reason: {{}}".format(failure_message))

and this was causing the issue with a misleading error.

I had to make sure any single pair of curly braces in the python code were double braced to maintain single braces for when the formatting finished.

Priority answered 7/9, 2023 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.