Im trying to backtest a trading strategy and therefore I have split my data with TimeSeriesSplit. This creates 5 different pandas series for all columns with different lengths. I've managed to hardcode the first list with length = 16 into a pandas dataframe, but would like to make individual dataframes for all different lengths in my list.
The code is the following:
from sklearn.model_selection import TimeSeriesSplit
import yfinance as yf
data = yf.download("NVDA",start="2017-01-01", end="2017-04-30")
data
tss = TimeSeriesSplit(n_splits = 5)
column = []
for columns in data:
column.append(data[columns])
train_data = []
test_data = []
for i in column:
for train_index, test_index in tss.split(i):
train = i[train_index]
test = i[test_index]
train_data.append(train)
test_data.append(test)
# copied the list
testing = train_data.copy()
first_df = []
for i in testing:
#print(len(i))
if len(i) == 16:
first_df.append(i)
# this works to get the dataframe back with different lengths
new_df = pd.DataFrame(first_df)
test = new_df.transpose()
test
The output from "test" is correct for the first split of training data, but how can I do this with all 5 different lengths at once?