Convert Strings To Dates


# Load libraries
import numpy as np
import pandas as pd

Create Strings

# Create strings
date_strings = np.array(['03-04-2005 11:35 PM',
                         '23-05-2010 12:01 AM',
                         '04-09-2009 09:09 PM'])

Convert Strings To Timestamps

If errors="coerce" then any problem will not raise an error (the default behavior) but instead will set the value causing the error to NaT (i.e. a missing value).

Code Description Example
Full year `2001`
Month w/ zero padding `04`
Day of the month w/ zero padding `09`
Hour (12hr clock) w/ zero padding `02`
AM or PM `AM`
Minute w/ zero padding `05`
Second w/ zero padding `09`
# Convert to datetimes
[pd.to_datetime(date, format="%d-%m-%Y %I:%M %p", errors="coerce") for date in date_strings]
[Timestamp('2005-04-03 23:35:00'),
 Timestamp('2010-05-23 00:01:00'),
 Timestamp('2009-09-04 21:09:00')]