["bid_size", "ask_size", "bid_price", "ask_price"] groupby_cols = ["stock_id"] global_features_df = ( df.group_by(groupby_cols).agg(to_describe(columns)).sort("stock_id") ) global_features_df = global_features_df.with_columns( median_size=pl.col("bid_size_median").add(pl.col("ask_size_median")), std_size=pl.col("bid_size_std").add(pl.col("ask_size_std")), ptp_size=pl.col("bid_size_max").sub(pl.col("ask_size_min")), median_price=pl.col("bid_price_median").add(pl.col("ask_price_median")), std_price=pl.col("bid_price_std").add(pl.col("ask_price_std")), ptp_price=pl.col("bid_price_max").sub(pl.col("ask_price_min")), ) return df.join(global_features_df, on="stock_id", how="left") 13