Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Date to timezone aware datetime in polars

Let's say I have

df = pl.DataFrame({
    "date": pl.Series(["2022-01-01", "2022-01-02"]).cast(pl.Date)
})

How do I localize that to a specific timezone and make it a datetime?

I tried:

df.select(pl.col('date').cast(pl.Datetime(time_zone='America/New_York')))

but that gives me

shape: (2, 1)
┌────────────────────────────────┐
│ date                           │
│ ---                            │
│ datetime[μs, America/New_York] │
╞════════════════════════════════╡
│ 2021-12-31 19:00:00 EST        │
│ 2022-01-01 19:00:00 EST        │
└────────────────────────────────┘

so it looks like it's starting from the presumption that the naïve datetimes are UTC and then applying the conversion. I set os.environ['TZ']='America/New_York' but I got the same result.

I looked through the polars config options in the API guide to see if there's something else to set but couldn't find anything about default timezone.

like image 618
Dean MacGregor Avatar asked Jan 24 '26 23:01

Dean MacGregor


1 Answers

As of polars 0.16.3, you can do:

df.select(
    pl.col('date').cast(pl.Datetime).dt.replace_time_zone("America/New_York")
)
like image 83
ritchie46 Avatar answered Jan 26 '26 12:01

ritchie46