Home > database >  cast datetime[ms] to datetime[ns] in polars
cast datetime[ms] to datetime[ns] in polars

Time:01-08

I'd like to cast a column with the type datetype[ms] to datetime[ns] in polars. Is there a easy way to do this? I think there's a problem with group_by_dynamic when using datetime[ms] and I'd like to test this

CodePudding user response:

s = pl.date_range(datetime(2021, 1, 1), datetime(2021, 1, 2), interval="3h", time_unit="ns")

# divide by 1 million is from ns to ms
(s.cast(int) // 1_000_000).cast(pl.Datetime).dt.and_time_unit("ms")
shape: (9,)
Series: '' [datetime[ms]]
[
    2021-01-01 00:00:00
    2021-01-01 03:00:00
    2021-01-01 06:00:00
    2021-01-01 09:00:00
    2021-01-01 12:00:00
    2021-01-01 15:00:00
    2021-01-01 18:00:00
    2021-01-01 21:00:00
    2021-01-02 00:00:00
]
  •  Tags:  
  • Related