Polars: Replace the Year in a DataFrame's Datetime Column

Advertisements

I have a polars DataFrame with a bunch of columns. One of them has datetime values (for example hourly data from 2017 – 2019). How can I replace the year of all the datetime values with a year I specify?

Original Datetime Column:

shape: (26280, 1)    
Index
datetime[ns]
2017-01-01 00:00:00
2017-01-01 01:00:00
2017-01-01 02:00:00
2017-01-01 03:00:00
...
2019-12-31 20:00:00
2019-12-31 21:00:00
2019-12-31 22:00:00
2019-12-31 23:00:00

Required Datetime Column:

shape: (26280, 1)
Index
datetime[ns]
2022-01-01 00:00:00
2022-01-01 01:00:00
2022-01-01 02:00:00
2022-01-01 03:00:00
...
2022-12-31 20:00:00
2022-12-31 21:00:00
2022-12-31 22:00:00
2022-12-31 23:00:00

>Solution :

I don’t think there’s anything built-in, but you could do

ser.dt.strftime('2022-%m-%d %H:%M:%S').str.strptime(pl.Datetime)

e.g.:

In [3]: ser = pl.Series(['2017-01-01 00:00:00', '2019-12-31 23:00:00']).str.strptime(pl.Datetime)

In [4]: ser
Out[4]:
shape: (2,)
Series: '' [datetime[μs]]
[
        2017-01-01 00:00:00
        2019-12-31 23:00:00
]

In [5]: ser.dt.strftime('2022-%m-%d %H:%M:%S').str.strptime(pl.Datetime)
Out[5]:
shape: (2,)
Series: '' [datetime[μs]]
[
        2022-01-01 00:00:00
        2022-12-31 23:00:00
]

Leave a Reply Cancel reply