This outstanding post illustrates quite clearly how to use the pandas cumsum()
DataFrame method to build a 3D tensor containing a column with lists of lists whose dimensions make them suitable to be used as time series input to an LSTM. I would like to do something very similar but with a rolling list of lists instead of a cumulative aggregation of lists.
For example. Say you had a DataFrame with 3 time series thus:
A B C
1 2 3
4 5 6
7 8 9
10 11 12
The article I linked to above, shows you how to use pandas cumsum()
to build a DataFrame column of nested lists that look like this:
[[1, 2, 3]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The key lines of python code that accomplish this are as follows:
input_cols = list(df.columns)
df['single_list'] = df[input_cols].apply(
tuple, axis=1).apply(list)
df['double_encapsulated'] = df.single_list.apply(
lambda x: [list(x)])
But I want a rolling window of lists, not a cumulative sum of lists. It should look like this:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[7, 8, 9], [10, 11, 12], [13, 14, 15]]
Can this be done with a Rolling object?