change format of date in pandas column

Pandas – Change Format of Date Column

When working with data, you might often encounter instances where your dates are not in the format the you want. For example, the dates are in “YYYY-MM-DD” format and you want them to be in “MM-DD-YYYY” format. In this tutorial, we will look at how to change the format of a date column in a pandas dataframe.

To change the date format of a column in a pandas dataframe, you can use the pandas series dt.strftime() function. Pass the format that you want your date to have. The following is the syntax:

# change the format to DD-MM-YYYY
df['Col'] = df['Col'].dt.strftime('%d-%m%Y')

Here, “Col” is the datetime column for which you want to change the format. The dt.strftime() function returns an array of formatted dates as strings.

Let’s look at the usage of this function with the help of some examples. First, let’s create a sample dataframe that we will be using throughout this tutorial.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'Name': ['Jim', 'Dwight', 'Pam', 'Angela', 'Michael'],
    'Birthday': ['1980-04-01', '1978-06-24', '1982-10-07', '1980-12-25', '1970-02-28']
})
# show the dataframe
print(df)

Output:

      Name    Birthday
0      Jim  1980-04-01
1   Dwight  1978-06-24
2      Pam  1982-10-07
3   Angela  1980-12-25
4  Michael  1970-02-28

We now have a dataframe storing names and birthdays of employees at an office. Let’s look the data type of the “Birthday” column using the pandas info() function.

# show data types of each column
df.info()

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Name      5 non-null      object
 1   Birthday  5 non-null      object
dtypes: object(2)
memory usage: 208.0+ bytes

You can see that the “Birthday” column is of type “object”. Let’s convert it to datetime, using the pandas to_datetime() function.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# covert to datetime
df['Birthday'] = pd.to_datetime(df['Birthday'])
# show the types 
df.info()

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column    Non-Null Count  Dtype         
---  ------    --------------  -----         
 0   Name      5 non-null      object        
 1   Birthday  5 non-null      datetime64[ns]
dtypes: datetime64[ns](1), object(1)
memory usage: 208.0+ bytes

Now that we have our datetime column, let’s go ahead and see examples of how to change the date format.

Let’s create a new column, “Birthday2” which stores the birthday in the MM-DD-YYYY format. That is, the date “1980-04-01” would be represented as “04-01-1980”. For this, pass the date format string '%m-%d-%Y to the dt.strftime() function.

# date in MM-DD-YYYY format
df['Birthday2'] = df['Birthday'].dt.strftime('%m-%d-%Y')
# display the dataframe
print(df)

Output:

      Name   Birthday   Birthday2
0      Jim 1980-04-01  04-01-1980
1   Dwight 1978-06-24  06-24-1978
2      Pam 1982-10-07  10-07-1982
3   Angela 1980-12-25  12-25-1980
4  Michael 1970-02-28  02-28-1970

In the date format string, %m represents the month as a zero-padded number, %d represents the day of the month as a zero-padded number, and %Y represents the year with century (that is, 2017 and not just 17, which is represented by %y).

Note that if you check the data type of the “Birthday2” column, it will be of “object” type since the dt.strftime() function returns formatted dates as strings.

# show data types of each column
df.info()

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Name       5 non-null      object        
 1   Birthday   5 non-null      datetime64[ns]
 2   Birthday2  5 non-null      object        
dtypes: datetime64[ns](1), object(2)
memory usage: 248.0+ bytes

Let’s create a new column, “Birthday3” which stores the birthday in the DD-MM-YYYY format. That is, the date “1980-04-01” would be represented as “01-04-1980”. For this, pass the date format string '%d-%m-%Y to the dt.strftime() function.

# date in DD-MM-YYYY format
df['Birthday3'] = df['Birthday'].dt.strftime('%d-%m-%Y')
# display the dataframe
print(df)

Output:

      Name   Birthday   Birthday2   Birthday3
0      Jim 1980-04-01  04-01-1980  01-04-1980
1   Dwight 1978-06-24  06-24-1978  24-06-1978
2      Pam 1982-10-07  10-07-1982  07-10-1982
3   Angela 1980-12-25  12-25-1980  25-12-1980
4  Michael 1970-02-28  02-28-1970  28-02-1970

The dates in the “Birthday3” column are in the DD-MM-YYYY.

Let’s create a new column, “Birthday3” which stores the birthday in the Month Day, Year format. That is, the date “1980-04-01” would be represented as “April 01, 1980”. For this, pass the date format string '%B %d, %Y to the dt.strftime() function.

# date in Month day, Year format
df['Birthday4'] = df['Birthday'].dt.strftime('%B %d, %Y')
# display the dataframe
print(df)

Output:

      Name   Birthday   Birthday2   Birthday3          Birthday4
0      Jim 1980-04-01  04-01-1980  01-04-1980     April 01, 1980
1   Dwight 1978-06-24  06-24-1978  24-06-1978      June 24, 1978
2      Pam 1982-10-07  10-07-1982  07-10-1982   October 07, 1982
3   Angela 1980-12-25  12-25-1980  25-12-1980  December 25, 1980
4  Michael 1970-02-28  02-28-1970  28-02-1970  February 28, 1970

The %B in the format string represents the month name in full. You can find the complete list of format codes that can be used in the strftime() function here.

For more on the pandas series dt.strftime() function, refer to its documentaion.

You might also be interested in – Pandas – Extract Year from a datetime column

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on formatting pandas dataframe –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top