Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing the difference between first and last values in a rolling window

I am using the Pandas rolling window tool on a one-column dataframe whose index is in datetime form.

I would like to compute, for each window, the difference between the first value and the last value of said window. How do I refer to the relative index when giving a lambda function? (in the brackets below)

df2 = df.rolling('3s').apply(...)
like image 609
Pandora Avatar asked Dec 23 '22 11:12

Pandora


1 Answers

IIUC:

In [93]: df = pd.DataFrame(np.random.randint(10,size=(9, 3)))

In [94]: df
Out[94]:
   0  1  2
0  7  4  5
1  9  9  3
2  1  7  6
3  0  9  2
4  2  3  7
5  6  7  1
6  1  0  1
7  8  4  7
8  0  0  9

In [95]: df.rolling(window=3).apply(lambda x: x[0]-x[-1])
Out[95]:
     0    1    2
0  NaN  NaN  NaN
1  NaN  NaN  NaN
2  6.0 -3.0 -1.0
3  9.0  0.0  1.0
4 -1.0  4.0 -1.0
5 -6.0  2.0  1.0
6  1.0  3.0  6.0
7 -2.0  3.0 -6.0
8  1.0  0.0 -8.0
like image 89
MaxU - stop WAR against UA Avatar answered Dec 28 '22 07:12

MaxU - stop WAR against UA