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

pd.to_datetime with multiple format

I need to get a timestamp out of multiple time-formats like:

df['date'] = [29.05.2023 01:00:00, 
              29.05.2023, 
              28.05.2023 23:00:00]

At 00:00:00 on every day the time is missing and only the date has been logged.
I need the timestamp to look like this:

df['date'] = [29.05.2023 01:00:00, 
              29.05.2023 00:00:00, 
              28.05.2023 23:00:00]

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

>Solution :

Use to_datetime with dayfirst=True parameter, for final ouput DD.MM.YYYY HH:MM:SS use Series.dt.strftime:

df = pd.DataFrame({'date':['29.05.2023 01:00:00', 
                           '29.05.2023', 
                           '28.05.2023 23:00:00',
                           '4.10.2023']})


df['date'] = pd.to_datetime(df['date'], dayfirst=True).dt.strftime('%d.%m.%Y %H:%M:%S')
print (df)
                  date
0  29.05.2023 01:00:00
1  29.05.2023 00:00:00
2  28.05.2023 23:00:00
3  04.10.2023 00: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