2

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!

Luis Carcamo
  • 31
  • 1
  • 3
  • Hi, why didn't inverting work? what did you get? You mean, reversing the series, right like in https://stackoverflow.com/questions/20444087/right-way-to-reverse-pandas-dataframe, right? – jottbe Aug 28 '19 at 06:44

1 Answers1

0

Interpolation is by default linear, if you want it to look forward try setting interpolation to higher:

df['TR %'].rolling(window=days, min_periods=1).quantile(0.25, interpolation='higher')
BeRT2me
  • 12,699
  • 2
  • 13
  • 31