1

Given a list where each element is a dataframe, i want to create sliding windows in order to train a lstm model, but the problem is an error occurs. Each dataframe is a time series with the 4 columns but different length.

import import_ipynb 
import split_data
import numpy as np

train, val, test = split_data.split_datasets() window_size = 5

window_datasets = [] for i in range(len(train)): window_datasets.append(create_sliding_windows(train[i], window_size=5, n_future=1))

def create_sliding_windows(df, window_size=5, n_future=1): trainX = [] trainY = [] for i in range(window_size, len(df) - n_future + 1): trainX.append(df[i - window_size:i, 0:df.shape[1]]) trainY.append(df[i + n_future - 1, i + n_future, 0])

return np.array(trainX), np.array(trainY)





The error i achieve is :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3802             try:
-> 3803                 return self._engine.get_loc(casted_key)
   3804             except KeyError as err:

~/anaconda3/lib/python3.8/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/anaconda3/lib/python3.8/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

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

During handling of the above exception, another exception occurred:

InvalidIndexError Traceback (most recent call last) <ipython-input-68-7cd0d5e7624d> in <module> 6 window_datasets = [] 7 for i in range(len(train)): ----> 8 window_datasets.append(create_sliding_windows(train[i], window_size=5, n_future=1)) 9 10

<ipython-input-66-9d44549be273> in create_sliding_windows(df, window_size, n_future) 3 trainY = [] 4 for i in range(window_size, len(df) - n_future + 1): ----> 5 trainX.append(df[i - window_size:i, 0:df.shape[1]]) 6 trainY.append(df[i + n_future - 1, i + n_future, 0]) 7

~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in getitem(self, key) 3803 if self.columns.nlevels > 1: 3804 return self._getitem_multilevel(key) -> 3805 indexer = self.columns.get_loc(key) 3806 if is_integer(indexer): 3807 indexer = [indexer]

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3808 # InvalidIndexError. Otherwise we fall through and re-raise 3809 # the TypeError. -> 3810 self._check_indexing_error(key) 3811 raise 3812

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in _check_indexing_error(self, key) 5966 # if key is not a scalar, directly raise an error (the code below 5967 # would convert to numpy arrays and raise later any way) - GH29926 -> 5968 raise InvalidIndexError(key) 5969 5970 @cache_readonly

InvalidIndexError: (slice(0, 5, None), slice(0, 4, None))

heyoka955
  • 13
  • 4

0 Answers0