figure - percentile rank of aapl return over year window user-defined moving window functions the rolling_apply function provides means to apply an array function of your own devising over moving window the only requirement is that the function produce single value ( reductionfrom each piece of the array for examplewhile we can compute sample quantiles using rolling_quantilewe might be interested in the percentile rank of particular value over the sample the scipy stats percentileof score function does just thisin [ ]from scipy stats import percentileofscore in [ ]score_at_ percent lambda xpercentileofscore( in [ ]result pd rolling_apply(returns aapl score_at_ percentin [ ]result plot( time series www it-ebooks info