polars truncate

For expression.

Divide the date/datetime range into buckets.

warning This command requires a plugin The polars truncate command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.

Command Type

plugin

Signature

> polars truncate {flags} (every)

Parameters

  • every: Period length for every interval (can be duration or str).

Input/output types:

input output
polars_expression polars_expression
polars_selector polars_expression

Examples

Truncate a series of dates by period length

> seq date -b 2025-01-01 --periods 4 --increment 6wk -o "%Y-%m-%d %H:%M:%S" | polars into-df | polars as-datetime "%F %H:%M:%S" --naive | polars select datetime (polars col datetime | polars truncate 5d37m | polars as truncated)
╭───┬─────────────┬─────────────╮
│ # │  datetime   │  truncated  │
├───┼─────────────┼─────────────┤
│ 0 │ 2 years ago │ 2 years ago │
│ 1 │ 2 years ago │ 2 years ago │
│ 2 │ a year ago  │ a year ago  │
│ 3 │ a year ago  │ a year ago  │
╰───┴─────────────┴─────────────╯

Notes

Each date/datetime is mapped to the start of its bucket using the corresponding local datetime. Note that weekly buckets start on Monday. Ambiguous results are localised using the DST offset of the original timestamp - for example, truncating ‘2022-11-06 01:30:00 CST’ by ‘1h’ results in ‘2022-11-06 01:00:00 CST’, whereas truncating ‘2022-11-06 01:30:00 CDT’ by ‘1h’ results in ‘2022-11-06 01:00:00 CDT’.

    See Notes in documentation for full list of compatible string values for `every`: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.truncate.html

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.