polars datetime-range for dataframe
Create a datetime range expression.
This command requires a plugin
The polars datetime-range 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 datetime-range {flags}
Flags
--start, -s {any}: Start of the datetime range expression. This supports a string, date, or datetime expression.--end, -e {any}: End of the datetime range expression. This supports a string, date, or datetime expression.--interval, -i {any}: Interval of the datetime range expression. This supports a string or duration expression.--closed, -c {string}: Closed window of the datetime range expression. This supports 'left', 'right', 'both', or 'none'.--time-unit {string}: Time unit for the output datetime. One of: ns, us, ms.--time-zone {string}: Time zone for the output datetime. E.g. 'UTC', 'America/New_York'.--eager: Eagerly collect the datetime range expression into a dataframe.
Input/output types:
| input | output |
|---|---|
| any | polars_expression |
| any | polars_dataframe |
Examples
Eagerly create a column of datetimes from a start, end, and interval
> polars datetime-range --start 2022-01-01 --end 2022-03-01 --interval 1mo --eager
╭───┬─────────────╮
│ # │ literal │
├───┼─────────────┤
│ 0 │ 4 years ago │
│ 1 │ 4 years ago │
│ 2 │ 4 years ago │
╰───┴─────────────╯Create a datetime range per group from the min and max of a date column
> [[date key]; [2024-01-01 one] [2024-01-02 one] [2024-01-01 two] [2024-01-03 two]]
| polars into-lazy
| polars group-by key
| polars agg (polars datetime-range --start (polars col date | polars min) --end (polars col date | polars max) --interval 1d | polars as date_range)
| polars collect
| polars sort-by key
╭───┬─────┬─────────────────────────────╮
│ # │ key │ date_range │
├───┼─────┼─────────────────────────────┤
│ 0 │ one │ ╭───┬─────────────────────╮ │
│ │ │ │ 0 │ 1704067200000000000 │ │
│ │ │ │ 1 │ 1704153600000000000 │ │
│ │ │ ╰───┴─────────────────────╯ │
│ 1 │ two │ ╭───┬─────────────────────╮ │
│ │ │ │ 0 │ 1704067200000000000 │ │
│ │ │ │ 1 │ 1704153600000000000 │ │
│ │ │ │ 2 │ 1704240000000000000 │ │
│ │ │ ╰───┴─────────────────────╯ │
╰───┴─────┴─────────────────────────────╯