3

I'm using pd.to_datetime() with format= argument which speeds up the processing as the function doesn't have to infer the format for each row.

However my format= argument isn't working for times with AM/PM:

pd.to_datetime('01/10/2017 10:15:17 PM',format = '%m/%d/%Y %H:%M:%S %p')

gives:

Timestamp('2017-01-10 10:15:17')

The time should have been 22:15:17 since the original time has 'PM' Is there a bug in my code or is there an issue with the function.

smci
  • 32,567
  • 20
  • 113
  • 146
RukTech
  • 5,065
  • 5
  • 22
  • 23
  • I've never tried it with AM/PM, but I added `infer_datetime_format=True` to my `pd.to_datetime()` code and it *significantly* decreased processing time on larger data sets. – elPastor Mar 02 '17 at 02:45
  • See the doc for [strftime formatting strings](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) – smci Dec 30 '20 at 10:14

1 Answers1

6

You need %I(01-12) instead of %H (00-23):

pd.to_datetime('01/10/2017 10:15:17 PM',format = '%m/%d/%Y %I:%M:%S %p')
​# Timestamp('2017-01-10 22:15:17')
Psidom
  • 209,562
  • 33
  • 339
  • 356