polars.Expr.dt.epoch#

Expr.dt.epoch(time_unit: EpochTimeUnit = 'us') Expr[source]#

Get the time passed since the Unix EPOCH in the give time unit.

Parameters:
time_unit{‘ns’, ‘us’, ‘ms’, ‘s’, ‘d’}

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), eager=True)}
... )
>>> df.select(
...     [
...         pl.col("date"),
...         pl.col("date").dt.epoch().alias("epoch_ns"),
...         pl.col("date").dt.epoch(time_unit="s").alias("epoch_s"),
...     ]
... )
shape: (3, 3)
┌─────────────────────┬─────────────────┬───────────┐
│ date                ┆ epoch_ns        ┆ epoch_s   │
│ ---                 ┆ ---             ┆ ---       │
│ datetime[μs]        ┆ i64             ┆ i64       │
╞═════════════════════╪═════════════════╪═══════════╡
│ 2001-01-01 00:00:00 ┆ 978307200000000 ┆ 978307200 │
│ 2001-01-02 00:00:00 ┆ 978393600000000 ┆ 978393600 │
│ 2001-01-03 00:00:00 ┆ 978480000000000 ┆ 978480000 │
└─────────────────────┴─────────────────┴───────────┘