Rohit Lamba K
Rohit Lamba K

Reputation: 510

Rolling Rank of a Column in Pandas with min_periods

I have a dataframe where I want to calculate rolling rank with window=10 and a min_periods=3.

Base Code:

df['RHigh'] = df.High.rolling(window=10).apply(lambda x: pd.Series(x).rank(ascending=False).values[-1])

This is working fine. But if I am adding a min_periods=3

Revised Code:

df['RHigh'] = df.High.rolling(window=10, min_periods=3).apply(lambda x: pd.Series(x).rank(ascending=False).values[-1])

Its giving me the same output of base code only.

Anything I am missing..?

Upvotes: 0

Views: 242

Answers (1)

perl
perl

Reputation: 9941

They should be different, see below:

np.random.seed(42)
df = pd.DataFrame({'High': np.random.uniform(0, 1, 100)})

df['RHigh_base'] = df.High.rolling(window=10).apply(
    lambda x: pd.Series(x).rank(ascending=False).values[-1])

df['RHigh_min_periods'] = df.High.rolling(window=10, min_periods=3).apply(
    lambda x: pd.Series(x).rank(ascending=False).values[-1])

df.head()

Output:

       High  RHigh_base  RHigh_min_periods
0  0.374540         NaN                NaN
1  0.950714         NaN                NaN
2  0.731994         NaN                2.0
3  0.598658         NaN                3.0
4  0.156019         NaN                5.0

Upvotes: 1

Related Questions