Reputation: 5107
I have a data-frame (df
) which looks like:
A B C D
date
2014-02-25 2.41 1.0 20712.0 324.90415
2014-02-26 2.41 1.0 11400.0 324.90415
2014-02-27 2.40 1.0 4370.0 323.55600
2014-02-28 2.37 1.0 51943.0 319.51155
2014-03-03 2.40 1.0 27902.0 323.55600
: : : :
2015-03-16 2.39 1.0 18059.0 322.20785
2015-03-17 2.40 1.0 24346.0 323.55600
2015-03-18 2.35 1.0 344171.0 316.81525
2015-03-19 2.35 1.0 15247.0 316.81525
2015-03-20 2.35 1.0 90217.0 316.81525
I would like to calculate the rolling variance for Column A for the last 5 dates (2015-03-16 - 2015-03-20) looking back at the last 260 days for each calculation.
I can see to calculate variance I can use df['A'].var()
. I could create a new data-frame that would just have the last 260 days but was wondering if there was a more elegant way to calculate the 260 days variance for the last 5 dates in the data-frame for column A?
Upvotes: 0
Views: 545
Reputation: 150785
Try:
variance = df.A.rolling(260).var()
which gives you variances for the whole data. Then you can do
variance[-5:]
Upvotes: 1