How to Convert Strings to Datetime in a pandas DataFrame

In this tutorial, you will learn how to convert strings to datetime in a DataFrame column.

TLDR solution

df['date'] = pd.to_datetime(df['date'], format='look at string')

Example 1: Convert Date Strings

Suppose, you have the following DataFrame.

import pandas as pd

data = {'date': ['01/15/2021', '02/15/2021', '03/15/2021']}

df = pd.DataFrame(data)

print(df)
print(df.dtypes)
         date
0  01/15/2021
1  02/15/2021
2  03/15/2021
date    object
dtype: object

The date column is of type object/string. Notice that the dates are formatted as mm/dd/yyyy.

Thus, to convert it to pandas datetime object, use to_datetime function:

df['date'] = pd.to_datetime(df['date'], format='%m/%d/%Y')
# where %m = double-digit mont, %d = double-digit day, %Y four-digit year

print(df.dtypes)
date    datetime64[ns]
dtype: object

Example 2: Convert Timestamp Strings

That's it! You just converted a column of date string to a datetime column.