Divide the date/datetime range into buckets.
Ambiguous results are localised using the DST offset of the original timestamp -
for example, rounding '2022-11-06 01:20:00 CST' by '1h' results in
'2022-11-06 01:00:00 CST', whereas rounding '2022-11-06 01:20:00 CDT' by
'1h' results in '2022-11-06 01:00:00 CDT'.
Expr Expression of data type :class:Date or :class:Datetime.
The every argument is created with the following small string formatting language:
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
--------
const df = pl.DataFrame([
pl.Series("datetime", [
new Date(Date.parse("2020-01-01T01:30:00.002+00:00")),
new Date(Date.parse("2020-01-01T02:02:01.030+00:00")),
new Date(Date.parse("2020-01-01T04:42:20.001+00:00")),
])]);
>>> df.select("datetime", pl.col("datetime").dt.round("1h").alias("hr0"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr0 │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:30:00.002 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 05:00:00 │
└─────────────────────────┴─────────────────────┘
>>> df.select("datetime", pl.col("datetime").dt.round("30m").alias("hr30m"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr30m │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:30:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 05:00:00 │
└─────────────────────────┴─────────────────────┘
Format Date/datetime with a formatting rule: See chrono strftime/strptime.
Divide the date/datetime range into buckets. Each date/datetime is mapped to the start of its bucket using the corresponding local datetime. Note that:
'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'.The size of each bucket.
The every argument is created with
the following string language:
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
Expr Expression of data type :class:Date or :class:Datetime.
--------
const df = pl.DataFrame([
pl.Series("datetime", [
new Date(Date.parse("2020-01-01T01:32:00.002+00:00")),
new Date(Date.parse("2020-01-01T02:02:01.030+00:00")),
new Date(Date.parse("2020-01-01T04:42:20.001+00:00")),
])]);
>>> df.select("datetime", pl.col("datetime").dt.truncate("1h").alias("hr0"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr0 │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:00:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 04:00:00 │
└─────────────────────────┴─────────────────────┘
>>> df.select("datetime", pl.col("datetime").dt.truncate("30m").alias("hr30m"));
shape: (3, 2)
┌─────────────────────────┬─────────────────────┐
│ datetime ┆ hr30m │
│ --- ┆ --- │
│ datetime[ms] ┆ datetime[ms] │
╞═════════════════════════╪═════════════════════╡
│ 2020-01-01 01:32:00.002 ┆ 2020-01-01 01:30:00 │
│ 2020-01-01 02:02:01.030 ┆ 2020-01-01 02:00:00 │
│ 2020-01-01 04:42:20.001 ┆ 2020-01-01 04:30:00 │
└─────────────────────────┴─────────────────────┘
DateTime functions for Series