How to add rolling KPI's to original dataframe in polars? when I do group by, I am not seeing an index and so cant join? I want to keep all original columns in dataframe intact but add rolling kpi to the dataframe?
Pandas code:
groups_df = df[mask_for_filter].groupby(['group_identifier'])
rolling_kpi = groups_df[['col_1', 'col_2']].rolling(15, min_periods=1, center=True).median().reset_index(level='group_identifier').sort_index()
df.loc[mask_for_filter, 'col_1_median'] = rolling_kpi['col_1']
df.loc[mask_for_filter, 'col_2_median'] = rolling_kpi['col_2']
Polars:
df = df.filter(mask_for_filter).group_by('group_identifier').agg(
col_1_median=pl.col('col_1').rolling_median(15, min_periods=1, center=True),
col_2_median=pl.col('col_2').rolling_median(15, min_periods=1, center=True))
Code: result_df should be same as df, except that with extra rolling median columns which is not happening in above....plus there is no index so can't merge/join
import polars as pl
import numpy as np
np.random.seed(0)
data = {
'group_identifier': np.random.choice(['A', 'B', 'C'], 100),
'col_1': np.random.randn(100).round(2),
'col_2': np.random.randn(100).round(2),
'other_col': np.random.randn(100).round(2)
}
df = pl.DataFrame(data)
mask_for_filter = df['col_1'] > 0
result_df = df.filter(mask_for_filter).group_by('group_identifier').agg(
col_1_median=pl.col('col_1').rolling_median(15, min_periods=1, center=True),
col_2_median=pl.col('col_2').rolling_median(15, min_periods=1, center=True)
)
mask_for_avgis not defined anywhere.dfor asresult_df. If the former: What rolling median should be assigned to rows indfwithpl.col(col_1) <= 0.