Following is my dataframe. I am trying to calculate rolling 5 period percent rank of ATR
. RollingPercentRank
is my desired output.
symbol Day time ATR RollingPercentRank
316356 SPY 11/29/2018 10:35:00 0.377880 NaN
316357 SPY 11/29/2018 10:40:00 0.391092 NaN
316358 SPY 11/29/2018 10:45:00 0.392983 NaN
316359 SPY 11/29/2018 10:50:00 0.399685 NaN
316360 SPY 11/29/2018 10:55:00 0.392716 0.2
316361 SPY 11/29/2018 11:00:00 0.381445 0.2
316362 AAPL 11/29/2018 11:05:00 0.387300 NaN
316363 AAPL 11/29/2018 11:10:00 0.390570 NaN
316364 AAPL 11/29/2018 11:15:00 0.381313 NaN
316365 AAPL 11/29/2018 11:20:00 0.398182 NaN
316366 AAPL 11/29/2018 11:25:00 0.377364 0.6
316367 AAPL 11/29/2018 11:30:00 0.373627 0.2
As of the 5th row, I want to apply the percent rank function to all 5 previous values(1st row to 5th row) of ATR
within a group. And as of the 6th row, I want to again apply the rank function to all 5 previous values(2nd row to 6th row) of ATR
.
I have tried the following which gives a "'numpy.ndarray' object has no attribute 'rank' " error.
df['RollingPercentRank'] = df.groupby(['symbol'])['ATR'].rolling(window=5,min_periods=5,center=False).apply(lambda x: x.rank(pct=True)).reset_index(drop=True)