You’re running a store and want to know the average of your sales every 3 days to track whether your sales are dipping. Below is a dataframe showing sales on specific weekdays.
3-day moving average
You can calculate the 3-day moving average in Polars using the expression rolling_mean like this:
Notice that the first 2 rows in Rol_Mean are null. That’s because the window size of 3 is not met on the first and second roll. If you don’t want null values, you can use the parameter min_samples, which determines the number of values that should be non-null before calculating the average. Let’s set it to 1.