polars.from_epoch#

polars.from_epoch(column: str | polars.internals.expr.expr.Expr, unit: Literal['ns', 'us', 'ms', 's', 'd'] = 's') Expr[source]#
polars.from_epoch(column: Union[Series, Sequence[int]], unit: Literal['ns', 'us', 'ms', 's', 'd'] = 's') Series

Utility function that parses an epoch timestamp (or Unix time) to Polars Date(time).

Depending on the unit provided, this function will return a different dtype: - unit=”d” returns pl.Date - unit=”s” returns pl.Datetime[“us”] (pl.Datetime’s default) - unit=”ms” returns pl.Datetime[“ms”] - unit=”us” returns pl.Datetime[“us”] - unit=”ns” returns pl.Datetime[“ns”]

Parameters:
column

Series or expression to parse integers to pl.Datetime.

unit

The unit of the timesteps since epoch time.

Examples

>>> df = pl.DataFrame({"timestamp": [1666683077, 1666683099]}).lazy()
>>> df.select(pl.from_epoch(pl.col("timestamp"), unit="s")).collect()
shape: (2, 1)
┌─────────────────────┐
│ timestamp           │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2022-10-25 07:31:17 │
│ 2022-10-25 07:31:39 │
└─────────────────────┘

The function can also be used in an eager context by passing a Series.

>>> s = pl.Series([12345, 12346])
>>> pl.from_epoch(s, unit="d")
shape: (2,)
Series: '' [date]
[
        2003-10-20
        2003-10-21
]