Home > database >  Format string to datetime in Pandas without zero padding, AM/PM and UTC
Format string to datetime in Pandas without zero padding, AM/PM and UTC

Time:06-28

My csv file contains the date and time values like below. I want to convert this to a datetime format in pandas but I keep getting errors.
I then want to convert it to a 24 hour format with zero padding and without the mention UTC

I already tried : format="%m/%d/%Y %I:%M %p"

10/12/2021 10:25 AM UTC   
9/28/2021 8:51 AM UTC   
7/27/2021 9:45 AM UTC   
2/2/2022 7:10 PM UTC 

Desired output:

10/12/2021 10:25   
09/28/2021 08:51   
07/27/2021 09:45   
02/02/2022 19:10 

Any help is greatly appreciated

CodePudding user response:

You can use %m/%d/%Y %H:%M:

pd.to_datetime(df['date'], dayfirst=False).dt.strftime('%m/%d/%Y %H:%M')

output:

0    10/12/2021 10:25
1    09/28/2021 08:51
2    07/27/2021 09:45
3    02/02/2022 19:10
Name: date, dtype: object

used input:

df = pd.DataFrame({'date': ['10/12/2021 10:25 AM UTC',
                            '9/28/2021 8:51 AM UTC',
                            '7/27/2021 9:45 AM UTC',
                            '2/2/2022 7:10 PM UTC']})
  • Related