polars.from_epoch#
- polars.from_epoch( ) Expr | Series[source]#
Utility function that parses an epoch timestamp (or Unix time) to Polars Date(time).
Depending on the
time_unitprovided, this function will return a different dtype:time_unit=”d” returns pl.Date
time_unit=”ns” returns pl.Datetime[“ns”]
otherwise returns pl.Datetime[“us”] (Polars’ default time unit)
- Parameters:
- column
Series or Expression to parse to Datetime.
- time_unit
The unit of time of the timesteps since epoch time.
Examples
Convert from integer seconds:
>>> df = pl.LazyFrame({"ts": [1666683077, 1666683099]}) >>> df.select(pl.from_epoch(pl.col("ts"), time_unit="s")).collect() shape: (2, 1) ┌─────────────────────┐ │ ts │ │ --- │ │ datetime[μs] │ ╞═════════════════════╡ │ 2022-10-25 07:31:17 │ │ 2022-10-25 07:31:39 │ └─────────────────────┘
Convert from fractional seconds:
>>> df = pl.LazyFrame({"ts": [-609066.723456, 1066445333.8888, 3405071999.987654]}) >>> df.select(pl.from_epoch(pl.col("ts"), time_unit="s")).collect() shape: (3, 1) ┌────────────────────────────┐ │ ts │ │ --- │ │ datetime[μs] │ ╞════════════════════════════╡ │ 1969-12-24 22:48:53.276544 │ │ 2003-10-18 02:48:53.888800 │ │ 2077-11-25 13:19:59.987654 │ └────────────────────────────┘
The function can also be used in an eager context by passing a Series or sequence of int/float values.
>>> s = pl.Series([12345, 12346]) >>> pl.from_epoch(s, time_unit="d") shape: (2,) Series: '' [date] [ 2003-10-20 2003-10-21 ]