I've got a dataframe like
xs = pd.DataFrame({
'batch1': {
'timestep1': [1, 2, 3],
'timestep2': [3, 2, 1]
}
}).T
and I want to convert it into a numpy array of shape (batch,timestep,feature). For xs that should be (1,2,3).
The issue is panda only knows the 2D shape, so to_numpy produces a 2D shape.
xs.to_numpy().shape # (1, 2)
Similarly, this prevents using np.reshape because numpy doesn't seem to see the innermost dimension as an array
xs.to_numpy().reshape((1,2,3)) # ValueError: cannot reshape array of size 2 into shape (1,2,3)
[Edit] Add context on how the dataframe arrived in this state.
The dataframe originally started as
xs = pd.DataFrame({
('batch1','timestep1'): {
'feature1': 1,
'feature2': 2,
'feature3': 3
},
('batch1', 'timestep2'): {
'feature1': 3,
'feature2': 2,
'feature3': 1
}
}
).T
which I decomposed into the nested list/array using
xs.apply(pd.DataFrame.to_numpy, axis=1).unstack()



xs.to_numpy().shape # (1, 2)where if you check the innermost dimension you can see the correct length:xs.to_numpy()[0][0].shape # (3,). So I'm stuck trying to promote that innermost shape up one level, I think .