Home > Enterprise >  about an strange error on Slicing a DataFrame
about an strange error on Slicing a DataFrame

Time:01-25

I'm facing with a Multivariate AI algoritm copied on the net.

After few adaption of my 2D data to the algoritm, I'm encounted in a strange error when computing TrainX and TrainY:

TypeError: '(slice(0, 14, None), slice(0, 5, None))' is an invalid key

considering the following variables: n_past = 10,n_future = 1, df_for_training.shape == 343 x 6

for i in range(n_past, len(df_for_training) - n_future  1):
#    print(df_for_training[i - n_past:i, 0:df_for_training.shape[1]])
    
    print(i - n_past,'-',i, " ", 0,'-',df_for_training.shape[1])
    print(i   n_future - 1,'-',i   n_future, " ", 5)
    trainX.append(df_for_training[i - n_past:i, 0:df_for_training.shape[1]])
## Lo 5 di trainY puo essere scelto in funzione dell'output da predirre desiderato !! ##     
    trainY.append(df_for_training[i   n_future - 1:i   n_future, 5])

Despite the "originality" of the code and the slicing seams apparently coherent (not exiding) with 2D's DataFrame size, I thus don't understand if such error is still of a size kind or because of a wrong slicing technique ?

thanks

CodePudding user response:

The [] for dataframes only slices rows, to slice rows and columns, you need the iloc operator:

import pandas as pd
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},

          {'a': 100, 'b': 200, 'c': 300, 'd': 400},

          {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]

df = pd.DataFrame(mydict)
print(df.head())
print(df.iloc[1:3, 2:4])

Output:

      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000
      c     d
1   300   400
2  3000  4000

As a general tip: Always create small examples where you test your operations, as that usually helps to pinpoint the issue. Also very useful for asking questions here, as they can be easily copy/pasted by anyone reading your question

  •  Tags:  
  • Related