polars native way to convert unix timestamp to date
Asked Answered
A

1

5

I'm working with some data frames that contain Unix epochs in ms, and would like to display the entire timestamp series as a date. Unfortunately, the docs did not help me find a polars native way to do this, and I'm reaching out here. Solutions on how to do this in Python and also in Rust would brighten my mind and day.

With pandas, for example, such things were possible:

pd.to_datetime(pd_df.timestamp, unit="ms")
# or to convert the whole col
pd_df.timestamp = pd.to_datetime(pd_df.timestamp, unit="ms")

I could loop over the whole thing and do something like I'm doing here for a single entry in each row.

datetime.utcfromtimestamp(pl_df["timestamp"][0] / 1000).strftime("%Y-%m-%d")

If I were to do this in Rust, I would then use something like chrono to convert the ts to a date. But I don't think looping over each row is a good solution.

For now, as the best way I have found to help me is to convert pd_df = pl_df.to_pandas() and do it in pandas.

Aloeswood answered 16/12, 2022 at 20:29 Comment(0)
U
13

Adapting the answer of @jqurious. Polars has a dedicated from_epoch function for this:

(pl.DataFrame({"timestamp": [1397392146866, 1671225446800]})
   .with_columns(
       pl.from_epoch("timestamp", time_unit="ms")
   )
)
shape: (2, 1)
┌─────────────────────────┐
│ timestamp               │
│ ---                     │
│ datetime[ms]            │
╞═════════════════════════╡
│ 2014-04-13 12:29:06.866 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2022-12-16 21:17:26.800 │
└─────────────────────────┘
Unsling answered 17/12, 2022 at 16:43 Comment(1)
this is outdated for the current version of polars (0.18.4), "with_column" is now with_columns, "unit" is now time_unitSunbeam

© 2022 - 2024 — McMap. All rights reserved.