Parsing String To Datetime While Accounting For Am/pm In Pandas
I am trying to parse a string in this format '2018 - 07 - 07 04 - AM' to pandas datetime using strftime format. However, It seems to me the format doesn't recognize the difference
Solution 1:
Since you're parsing a 12-hour time format, you will need %I
instead of %H
, otherwise the %p
specifier has no effect.
pd.to_datetime("2018 - 07 - 07 04 - PM", format='%Y - %m - %d %I - %p')
Timestamp('2018-07-07 16:00:00')
This behaviour is documented in the docs:
When used with the
strptime()
function, the%p
directive only affects the output hour field if the%I
directive is used to parse the hour.
Solution 2:
In my case, I got an error message when trying the proposed solutions:
ValueError: unconverted data remains: PM
This was solved by setting the locale to en_US
.
Post a Comment for "Parsing String To Datetime While Accounting For Am/pm In Pandas"