Skip to content

Compute a rolling standard deviation based on another column

Source code

Description

Compute a rolling standard deviation based on another column

Usage

<Expr>$rolling_std_by(
  by,
  window_size,
  ...,
  min_periods = 1,
  closed = "right",
  ddof = 1
)

Arguments

by This column must of dtype Date or Datetime.
window_size The length of the window. Can be a fixed integer size, or a dynamic temporal size indicated by the following string language:
  • 1ns (1 nanosecond)
  • 1us (1 microsecond)
  • 1ms (1 millisecond)
  • 1s (1 second)
  • 1m (1 minute)
  • 1h (1 hour)
  • 1d (1 day)
  • 1w (1 week)
  • 1mo (1 calendar month)
  • 1y (1 calendar year)
  • 1i (1 index count) If the dynamic string language is used, the by and closed arguments must also be set.
Ignored.
min_periods The number of values in the window that should be non-null before computing a result. If NULL, it will be set equal to window size.
closed Define which sides of the temporal interval are closed (inclusive). This can be either “left”, “right”, “both” or “none”.
ddof An integer representing "Delta Degrees of Freedom": the divisor used in the calculation is N - ddof, where N represents the number of elements. By default ddof is 1.

Details

If you want to compute multiple aggregation statistics over the same dynamic window, consider using $rolling() this method can cache the window size computation.

Value

Expr

Examples

library("polars")

df_temporal = pl$DataFrame(
  date = pl$datetime_range(as.Date("2001-1-1"), as.Date("2001-1-2"), "1h")
)$with_row_index("index")

df_temporal
#> shape: (25, 2)
#> ┌───────┬─────────────────────┐
#> │ index ┆ date                │
#> │ ---   ┆ ---                 │
#> │ u32   ┆ datetime[μs]        │
#> ╞═══════╪═════════════════════╡
#> │ 0     ┆ 2001-01-01 00:00:00 │
#> │ 1     ┆ 2001-01-01 01:00:00 │
#> │ 2     ┆ 2001-01-01 02:00:00 │
#> │ 3     ┆ 2001-01-01 03:00:00 │
#> │ 4     ┆ 2001-01-01 04:00:00 │
#> │ …     ┆ …                   │
#> │ 20    ┆ 2001-01-01 20:00:00 │
#> │ 21    ┆ 2001-01-01 21:00:00 │
#> │ 22    ┆ 2001-01-01 22:00:00 │
#> │ 23    ┆ 2001-01-01 23:00:00 │
#> │ 24    ┆ 2001-01-02 00:00:00 │
#> └───────┴─────────────────────┘
# Compute the rolling std with the temporal windows closed on the right (default)
df_temporal$with_columns(
  rolling_row_std = pl$col("index")$rolling_std_by("date", window_size = "2h")
)
#> shape: (25, 3)
#> ┌───────┬─────────────────────┬─────────────────┐
#> │ index ┆ date                ┆ rolling_row_std │
#> │ ---   ┆ ---                 ┆ ---             │
#> │ u32   ┆ datetime[μs]        ┆ f64             │
#> ╞═══════╪═════════════════════╪═════════════════╡
#> │ 0     ┆ 2001-01-01 00:00:00 ┆ null            │
#> │ 1     ┆ 2001-01-01 01:00:00 ┆ 0.707107        │
#> │ 2     ┆ 2001-01-01 02:00:00 ┆ 0.707107        │
#> │ 3     ┆ 2001-01-01 03:00:00 ┆ 0.707107        │
#> │ 4     ┆ 2001-01-01 04:00:00 ┆ 0.707107        │
#> │ …     ┆ …                   ┆ …               │
#> │ 20    ┆ 2001-01-01 20:00:00 ┆ 0.707107        │
#> │ 21    ┆ 2001-01-01 21:00:00 ┆ 0.707107        │
#> │ 22    ┆ 2001-01-01 22:00:00 ┆ 0.707107        │
#> │ 23    ┆ 2001-01-01 23:00:00 ┆ 0.707107        │
#> │ 24    ┆ 2001-01-02 00:00:00 ┆ 0.707107        │
#> └───────┴─────────────────────┴─────────────────┘
# Compute the rolling std with the closure of windows on both sides
df_temporal$with_columns(
  rolling_row_std = pl$col("index")$rolling_std_by("date", window_size = "2h", closed = "both")
)
#> shape: (25, 3)
#> ┌───────┬─────────────────────┬─────────────────┐
#> │ index ┆ date                ┆ rolling_row_std │
#> │ ---   ┆ ---                 ┆ ---             │
#> │ u32   ┆ datetime[μs]        ┆ f64             │
#> ╞═══════╪═════════════════════╪═════════════════╡
#> │ 0     ┆ 2001-01-01 00:00:00 ┆ null            │
#> │ 1     ┆ 2001-01-01 01:00:00 ┆ 0.707107        │
#> │ 2     ┆ 2001-01-01 02:00:00 ┆ 1.0             │
#> │ 3     ┆ 2001-01-01 03:00:00 ┆ 1.0             │
#> │ 4     ┆ 2001-01-01 04:00:00 ┆ 1.0             │
#> │ …     ┆ …                   ┆ …               │
#> │ 20    ┆ 2001-01-01 20:00:00 ┆ 1.0             │
#> │ 21    ┆ 2001-01-01 21:00:00 ┆ 1.0             │
#> │ 22    ┆ 2001-01-01 22:00:00 ┆ 1.0             │
#> │ 23    ┆ 2001-01-01 23:00:00 ┆ 1.0             │
#> │ 24    ┆ 2001-01-02 00:00:00 ┆ 1.0             │
#> └───────┴─────────────────────┴─────────────────┘