I am trying to create a rolling rank column for a float column. However the output is not as expected. Below I have an example: data = { 'time': pd.date_range(start='2025-01-01', periods=5, freq='H'), 'zone': ['A'] * 5, 'price': [1.0, 1.5, 1.7, 1.9, 2.0], } dummy_df = pd.DataFrame(data) class RollingRank: def __init__(self, rank_col: str, group_by_col: str, window_length: int, time_col: str): self.rank_col = rank_col self.time_col = time_col self.group_by_col = group_by_col self.window_length = window_length def fit_transform(self, df): df = df.sort_values(by=[self.group_by_col, self.time_col]) df.set_index(self.time_col, inplace=True) dfg = ( df.groupby(self.group_by_col) .rolling(window=f"{self.window_length}H", center=True, closed="both") .rank(method="min") ) df.loc[:, f"{self.rank_col}_rank"] = dfg[self.rank_col].values return df df_rank = RollingRank(rank_col="price", group_by_col="zone", window_length=3, time_col="time").fit_transform(dummy_df) As an output I get the rank [2.0, 3.0, 3.0, 3.0, 3.0] which does not make sense with center=True and closed="both". As an easy example for the middel row with the timestamp 2025-01-01 02:00:00 I would expect the rank to be 2 (and not 3 as in the output) as the values used for ranking would be [1.5, 1.7, 1.9]. Any ideas of what I am doing wrong? Continue reading...