polars truncate
for expression
Divide the date/datetime range into buckets.
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.
Signature
> polars truncate {flags} (every)
Parameters
every
: Period length for every interval (can be duration or str)
Input/output types:
input | output |
---|---|
any | any |
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 │ 3 months ago │ 3 months ago │
│ 1 │ 2 months ago │ 2 months ago │
│ 2 │ a month ago │ a month ago │
│ 3 │ in a week │ in 5 days │
╰───┴──────────────┴──────────────╯
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