polars.Expr.dt.timestamp#

Expr.dt.timestamp(tu: TimeUnit = 'us') Expr[source]#

Return a timestamp in the given time unit.

Parameters:
tu{‘us’, ‘ns’, ‘ms’}

Time unit.

Examples

>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 3)
>>> df = pl.DataFrame({"date": pl.date_range(start, stop, timedelta(days=1))})
>>> df.select(
...     [
...         pl.col("date"),
...         pl.col("date").dt.timestamp().alias("timestamp_ns"),
...         pl.col("date").dt.timestamp(tu="ms").alias("timestamp_ms"),
...     ]
... )
shape: (3, 3)
┌─────────────────────┬─────────────────┬──────────────┐
│ date                ┆ timestamp_ns    ┆ timestamp_ms │
│ ---                 ┆ ---             ┆ ---          │
│ datetime[μs]        ┆ i64             ┆ i64          │
╞═════════════════════╪═════════════════╪══════════════╡
│ 2001-01-01 00:00:00 ┆ 978307200000000 ┆ 978307200000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-02 00:00:00 ┆ 978393600000000 ┆ 978393600000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-03 00:00:00 ┆ 978480000000000 ┆ 978480000000 │
└─────────────────────┴─────────────────┴──────────────┘