Skip to content Skip to sidebar Skip to footer

Python: Handling Date/Time With AM/PM Information

In my Excel data, column 1 is titled 'Time'. It contains data and time information as follow: 7/26/2018 2:15:00 AM In my python code, I am trying to use pd.to_datetime to convert i

Solution 1:

Change your format to '%m/%d/%Y %I:%M:%S %p' works for me. Note the capital Y.

In related docs, %y means double-digit year, while %Y means four digit representation of year.

In addition, you should use %I instead of %H as the former is 12-hour and the latter is 24-hour.


Solution 2:

I would use dateutil.parser to convert string to datetime. It's pretty slick and will avoid any issues if your spreadsheet ever changes the time formatting.


Post a Comment for "Python: Handling Date/Time With AM/PM Information"