Is there a way to convert a pl.Series of type pl.Datetime from one timezone to another?

Question:

I am reading a parquet file with polars and would like to convert a column called datetime from type datetime[ms, America/New_York] to datetime[ns,UTC].
I can take the column out and do it in pandas, use tz_convert and add the column back to polars dataframe but would be nice if there was a way to do it in polars 🙂

Asked By: user20081698

||

Answers:

As of polars 0.14.14 there is:

pl.col("datetime").dt.with_time_zone which sets a timezone without modifying the underlying timestamp.

pl.col("datetime").dt.cast_time_zone which modifies the underlying timestamp by correcting from the current timezone the the given timezone.

EDIT

As of polars 0.16.3, there is:

pl.col("datetime").dt.convert_time_zone which sets a timezone without modifying the underlying timestamp.

pl.col("datetime").dt.replace_time_zone which modifies the underlying timestamp by correcting from the current timezone the the given timezone.

Answered By: ritchie46
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.