Home > front end >  Getting an error during reshaping the dataframe
Getting an error during reshaping the dataframe

Time:03-30

My code is:

last_steps = valid[['Close']].tail(60)
last_values = np.reshape(last_steps, ( -1, last_steps.shape[1], last_steps.shape[2]))

The second line rises this error:

IndexError                                Traceback (most recent call last)
C:\Users\UTKARS~1\AppData\Local\Temp/ipykernel_8696/2291656061.py in <module>
      1 last_steps = valid[['Close']].tail(60)
----> 2 last_values = np.reshape(last_steps, ( -1, last_steps.shape[1], last_steps.shape[2]))

IndexError: tuple index out of range

CodePudding user response:

May be you are providing indices wrong. Indices start from 0 in Python.

CodePudding user response:

you should check whether the last_steps variable has a 3-dimensional shape to call last_steps.shape[2]. you can just print it with print(last_steps.shape). shape function returns a tuple of the shape of an array.

it will print (5,2) if it's 2-dimensional. when you call last_steps.shape[0] it will returns 5

it will print (8,3,9) if it's 3-dimensional. when you call last_steps.shape[2] it will returns 9

and so on

Edit

After carefully looking into your code, I assume valid is a DataFrame. DataFrame is a 2-dimensional array. so I'm pretty sure when you execute last_steps.shape, it should return (60,1).

  • Related