def reshape_frames(dataframe, frame_length=12):
"""
Returns a new dataframe with the given frame length.
:param dataframe: A pandas DataFrame with one window per row.
:param frame_length: The desired number of windows for each feature frame. Must divide the number of windows in
*dataframe* evenly.
:return: A new pandas DataFrame with the desired window frame width. The columns of the new data-frame will be
multi-index so that
future concatenation of data frames align properly.
"""
# Assert that the length of the data frame is divisible by
# frame_length
n_windows, window_width = dataframe.shape
if n_windows % frame_length != 0:
raise ValueError("The dataframe has {} windows which"
" is not divisible by the frame"
" length {}".format(n_windows, frame_length))
values = dataframe.values
n_frames = n_windows / frame_length
frame_width = window_width * frame_length
window_columns = dataframe.columns
column_index = pd.MultiIndex.from_product([range(frame_length),
window_columns],
names=['window', 'feature'])
reshaped_frame = pd.DataFrame(data=values.reshape(n_frames,
frame_width),
columns=column_index)
reshaped_frame.sortlevel(axis=1)
return reshaped_frame
评论列表
文章目录