Change datetime Format in pandas DataFrame in Python (2 Examples)

 

This tutorial demonstrates how to modify the format of a datetime object in a pandas DataFrame in the Python programming language.

This article contains the sections below:

Let’s dive into the Python code!

 

Import Module & Create Example DataFrame

First, we have to import the pandas module to Python:

import pandas as pd

Next, we can create a pandas DataFrame with a datetime column as shown below:

data = pd.DataFrame({'Date':['1/11/2021', '2/12/2021', '3/13/2021', '4/14/2021']})
data['Date'] = pd.to_datetime(data['Date'])
print(data)
#         Date
# 0 2021-01-11
# 1 2021-02-12
# 2 2021-03-13
# 3 2021-04-14

The previous output illustrates the structure of our example data. It contains four rows and only one column. The values in this column are dates.

 

Example 1: Convert pandas DataFrame Column to Month/Day/Year Format

This example explains how to set a date column of a pandas DataFrame to a month, day, year order with slashes in between.

For this, we can use the strftime() function as shown below:

data_new1 = data
data_new1['Date_New'] = data_new1['Date'].dt.strftime('%m/%d/%Y')
print(data_new1)
#         Date    Date_New
# 0 2021-01-11  01/11/2021
# 1 2021-02-12  02/12/2021
# 2 2021-03-13  03/13/2021
# 3 2021-04-14  04/14/2021

The previous Python code has created a new DataFrame containing two columns, i.e. our input column and a new column with modified datetime format.

 

Example 2: Convert pandas DataFrame Column to Any Format

We can use the syntax of Example 1 to adjust a datetime variable as we want. All we have to change is the datetime format within the strftime function.

In this specific example, we’ll add three xxx between the date values:

data_new2 = data
data_new2['Date_New'] = data_new2['Date'].dt.strftime('%dxxx%mxxx%Y')
print(data_new2)
#         Date        Date_New
# 0 2021-01-11  11xxx01xxx2021
# 1 2021-02-12  12xxx02xxx2021
# 2 2021-03-13  13xxx03xxx2021
# 3 2021-04-14  14xxx04xxx2021

 

Video, Further Resources & Summary

Do you need more explanations on how to switch the datetime format in a pandas DataFrame in Python? Then you should have a look at the following video of Corey Schafer’s YouTube channel.

In his video, the speaker explains how to work with datetime and Time Series data in pandas.

 

 

In addition to the video, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to modify the datetime format in pandas DataFrame in the Python programming language. Please leave a comment below, in case you have further questions.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. Gottumukkala is a data analyst and programmer who helps to create tutorials on topics such as the datetime module in Python. You may find more information about Gottumukkala and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top