Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading