Search code examples
pythonpython-polars

Python Polars Parse Date from Epoch


How does one convert a column of i64 epoch strings into dates in polars?

I've got a column of i64 representing seconds since epoch and I'd like to parse them into polars native datetimes.


Solution

  • Update: Polars has since added a pl.from_epoch() helper method.

    df.with_columns(
        pl.from_epoch("epoch_seconds").alias("datetime")
    )
    
    shape: (2, 2)
    ┌───────────────┬─────────────────────┐
    │ epoch_seconds ┆ datetime            │
    │ ---           ┆ ---                 │
    │ i64           ┆ datetime[μs]        │
    ╞═══════════════╪═════════════════════╡
    │ 1648457740    ┆ 2022-03-28 08:55:40 │
    │ 1648457750    ┆ 2022-03-28 08:55:50 │
    └───────────────┴─────────────────────┘
    

    Original answer

    Polars' Datetime is represented as unix epoch in either, nanoseconds, microseconds or milliseconds. So with that knowledge we can convert the seconds to milliseconds and cast to Datetime.

    Finally we ensure polars uses the proper unit.

    df = pl.DataFrame({
        "epoch_seconds": [1648457740, 1648457740 + 10]
    })
    
    MILLISECONDS_IN_SECOND = 1000;
    
    df.select(
        (pl.col("epoch_seconds") * MILLISECONDS_IN_SECOND).cast(pl.Datetime("ms")).alias("datetime")
    )
    
    shape: (2, 1)
    ┌─────────────────────┐
    │ datetime            │
    │ ---                 │
    │ datetime[ms]        │
    ╞═════════════════════╡
    │ 2022-03-28 08:55:40 │
    │ 2022-03-28 08:55:50 │
    └─────────────────────┘