3

Let's say I have

df = pl.DataFrame({
    "date": pl.Series(["2022-01-01", "2022-01-02"]).str.strptime(pl.Date), "%Y-%m-%d")
})

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.

Dean MacGregor
  • 11,847
  • 9
  • 34
  • 72

1 Answers1

4

As of polars 0.16.3, you can do:

df.select(
    pl.col('date').cast(pl.Datetime).dt.replace_time_zone("America/New_York")
)

In previous versions (after 0.14.24), the syntax was

df.select(
    pl.col('date').cast(pl.Datetime).dt.tz_localize("America/New_York")
)
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
ritchie46
  • 10,405
  • 1
  • 24
  • 43