How to convert a pandas Series to datetime?

DataFrame column to datetime in Pandas

Step 1: Prepare your data

We will start by initializing a very simple DataFrame that we will se in our example:

import pandas as pd

start_date = ['3/1/2023', '4/1/2023', '5/1/2023', '6/1/2023']
students = [15, 12, 12, 23]

data = pd.DataFrame ((dict(start_date= start_date,  students = students)))

print(data.head())

Let’s look into the DataFrame contents:

start_datestudents
03/1/202315
14/1/202312
25/1/202312
36/1/202323

Step 2: Check your column data types

In this optional step we will look into the data types of the DataFrame columns:

data.dtypes

This will return a Series containing the data types of the DataFrame columns:

start_date    object
students       int64
dtype: object

As you can see, the start_date column, which contains data that although formatted as a date, contains actually strings.

Step 3: Convert the series to a datetime object

In order to convert the start_date to the datetime format, in our case in yyyy-mm-dd format, you have a couple options:

Use the astype() method:

data['start_date'].astype('datetime64')

Yse the pd.to_datetime function:

Both will cast your series of strings to the datetime object:

0   2023-03-01
1   2023-04-01
2   2023-05-01
3   2023-06-01
Name: start_date, dtype: datetime64[ns]

Step 4: Persist your changes in the DataFrame

In order to persist your changes simple re-assign the converted Series to the DataFrame, for example:

data['start_date'] = data['start_date'].astype('datetime64')

Now you can check your DataFrame data types:

data.dtypes

Note that start date data type is datetime:

start_date    datetime64[ns]
students               int64
dtype: object

Change datetime format and convert to string

We can use the strftime formatter to format a Series of datetime values. The result is a Series of objects (strings)

pd.to_datetime(data['start_date']).dt.strftime('%d/%m/%y')

Troubleshooting

If you try to call strftime on a Series of objects you will receive the following error:

AttributeError: Can only use .dt accessor with datetimelike values

You can fix this error by calling strftime only on datatime objects as shown above.