2

I have a pandas df that looks like this for example:

%diff          

-0.087704164    
0.003908466 
-0.032150706    
-0.035684163    
0.001682029 
-0.072205803    
0.031636864 
-0.069263158    
-0.214883511    
-0.109286469    
0.274932615 
-0.016913319    
-0.075268817    
0.191906977 
0.043861703 
-0.048598131    
0.01280943  
0.014509621 
0.075564054 
-0.024034701    
0.009107468     
0.023465704     

I want to calculate the square root of 252 multiple by the standard deviation of the last 20 values in the column '%diff.

    %diff          std      

    -0.087704164    
    0.003908466 
    -0.032150706    
    -0.035684163    
    0.001682029 
    -0.072205803    
    0.031636864 
    -0.069263158    
    -0.214883511    
    -0.109286469    
    0.274932615 
    -0.016913319    
    -0.075268817    
    0.191906977 
    0.043861703 
    -0.048598131    
    0.01280943  
    0.014509621 
    0.075564054 
   -0.024034701     165.9%
    0.009107468     163.2%
    0.023465704     163.4%

The code I tried is:

df1['std'] = 252**(1.0/2) * df1['%diff'].std().split(20)

But I get an unsupported operand error

1 Answer 1

3

You need rolling with a window of 20 and std like

df1['std'] = 252**(1.0/2) * df1.rolling(20)['%diff'].std()
print (df1)
       %diff       std
0  -0.087704       NaN
1   0.003908       NaN
2  -0.032151       NaN
3  -0.035684       NaN
4   0.001682       NaN
5  -0.072206       NaN
6   0.031637       NaN
7  -0.069263       NaN
8  -0.214884       NaN
9  -0.109286       NaN
10  0.274933       NaN
11 -0.016913       NaN
12 -0.075269       NaN
13  0.191907       NaN
14  0.043862       NaN
15 -0.048598       NaN
16  0.012809       NaN
17  0.014510       NaN
18  0.075564       NaN
19 -0.024035  1.659144
20  0.009107  1.631865
21  0.023466  1.634266
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.