Is there a way to do a rolling standard deviation in a dataframe where say in a 200 row dataframe starting at row 0 and going to 200 in increments of say 15?

df['SD1'] = df['ratio_div'].rolling(window =-15, min_periods= -15).std()

You cannot do a negitive rolling window. What other ways can i make this happen? I have tried doing it with 15 and 15 and the SD is Nan for the first 15. If it was taking index 0 of ratio_dev to index 14 there should be a value to go in SD1 index 0.

ratio_div at index 0 is always the most current and should show a std of itself and the prior 14. This does not seem to work with the options i tried. Any ideas?

                time    close   num ratio   ratio_div       SD1
    0   4/29/2019 8:24  1.1159  386 0   0
    1   4/29/2019 8:23  1.1159  620 -1  -0.000448029
    2   4/29/2019 8:22  1.116   648 1   0.000448069
    3   4/29/2019 8:21  1.1159  606 0   0
    4   4/29/2019 8:20  1.1159  638 1   0.000448109
    5   4/29/2019 8:19  1.1158  607 -2  -0.000896057
    6   4/29/2019 8:18  1.116   679 1   0.000448069
    7   4/29/2019 8:17  1.1159  676 1   0.000448109
    8   4/29/2019 8:16  1.1158  680 -1  -0.000448069
    9   4/29/2019 8:15  1.1159  694 1   0.000448109
   10   4/29/2019 8:14  1.1158  776 1   0.000448149
   11   4/29/2019 8:13  1.1157  937 -2  -0.000896138
   12   4/29/2019 8:12  1.1159  628 0   0
   13   4/29/2019 8:11  1.1159  613 0   0
   14   4/29/2019 8:10  1.1159  655 -1  -0.000448029
   15   4/29/2019 8:09  1.116   842 -1  -0.000447989
   16   4/29/2019 8:08  1.1161  820 -3  -0.001343604
   17   4/29/2019 8:07  1.1164  661 2   0.000895897
   18   4/29/2019 8:06  1.1162  640 0   0
   19   4/29/2019 8:05  1.1162  590 -1  -0.000447908
   20   4/29/2019 8:04  1.1163  569 1   0.000447948
   21   4/29/2019 8:03  1.1162  603 0   0

1 Answers

0
Gluons On

This solves what i was trying to do here. Just need to apply it to each row....which is where i am having my issues now.

def eric_std(idxs):            
    x=idxs
    y=idxs+14
    result = (df.loc[x:y,['ratio_div']].std()).values[0]        
return result