Title: Convert Strings To Dates
Slug: convert_strings_to_dates
Summary: How to convert stings to dates for machine learning in Python.
Date: 2017-09-11 12:00
Category: Machine Learning
Tags: Preprocessing Dates And Times
Authors: Chris Albon

Preliminaries


In [1]:
# Load libraries
import numpy as np
import pandas as pd

Create Strings


In [2]:
# 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`

In [3]:
# Convert to datetimes
[pd.to_datetime(date, format="%d-%m-%Y %I:%M %p", errors="coerce") for date in date_strings]


Out[3]:
[Timestamp('2005-04-03 23:35:00'),
 Timestamp('2010-05-23 00:01:00'),
 Timestamp('2009-09-04 21:09:00')]