v Convert Strings To Dates - Machine Learning

Convert Strings To Dates

Preliminaries

# 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
`%Y` Full year `2001`
`%m` Month w/ zero padding `04`
`%d` Day of the month w/ zero padding `09`
`%I` Hour (12hr clock) w/ zero padding `02`
`%p` AM or PM `AM`
`%M` Minute w/ zero padding `05`
`%S` 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')]