Search code examples
pythonarrayspandasnumpypython-3.7

pandas data frame / numpy array - roll without aggregate function


rolling in python aggregates data:

x = pd.DataFrame([[1,'a'],[2,'b'],[3,'c'],[4,'d']], columns=['a','b'])
y = x.rolling(2).mean()
print(y)

gives:

     a  b
0  NaN  a
1  1.5  b
2  2.5  c
3  3.5  d

what I need is 3 dimension dataframes (or numpy arrays) shifting 3 samples by 1 step (in this example):

[
  [[1,'a'],[2,'b'],[3,'c']],
  [[2,'b'],[3,'c'],[4,'d']]
]

Whats the right way to do it for 900 samples shifting by 1 each step?


Solution

  • Using np.concantenate

    np.concatenate([x.values[:-1], 
                    x.values[1:]], axis=1)\
      .reshape([x.shape[0] - 1, x.shape[1], -1])