I was trying to calculate the quantile of a pandas series , I was using a for loop that looks forward in n rows and then calculate the quantile, but I think is an inefficient way to do that.
I tried using the rolling function but the function is calculating that quantile looking backward.
This is my old code:
def calculate_percentile(value,days):
   result = []
   for index in range(len(df)):
       end = index + days +1
       #print(index,end)
       true_range_percentage = df.iloc[index:end].reset_index()['TR %']
       true_range_percentage = df.iloc[index:].reset_index()['TR %'] \ if 
                          len(true_range_percentage) == 0 else true_range_percentage
       result.append(true_range_percentage.quantile(value))
   return result
And works well but it took so much time.
This is the new way that I think maybe is more efficient way, but instead of looking forward, gets the quantile from backward, I tried inverse the series but didn't work.
df['TR %'].rolling(window=days, min_periods=1).quantile(0.25)
Thank you!
 
    