Skip to main content

Python Pandas: How to Change the Pandas Datetime Format in Python

Working with dates and times is a fundamental part of data analysis, and pandas provides powerful tools for handling datetime data. By default, pandas represents dates in the YYYY-MM-DD format (e.g., 2020-12-08 for December 8, 2020). However, you'll often need to display dates in a different format - whether it's DD/MM/YYYY, MM-DD-YYYY, or a more human-readable style like December 08, 2020.

In this guide, you'll learn how to change the datetime format in pandas using the strftime() method, along with practical examples covering the most common formatting scenarios.

Understanding strftime() and Format Codes

The strftime() method (short for string format time) converts datetime objects into formatted strings. You pass a format string composed of special directive codes, each representing a component of the date or time.

Here are the most commonly used format codes:

CodeDescriptionExample
%YFull year (4 digits)2024
%yAbbreviated year (2 digits)24
%mMonth as zero-padded number01, 12
%BFull month nameJanuary
%bAbbreviated month nameJan
%dDay of the month (zero-padded)01, 31
%HHour (24-hour clock)14
%IHour (12-hour clock)02
%MMinute05
%SSecond09
%pAM/PMPM
%AFull weekday nameMonday
%aAbbreviated weekday nameMon
tip

You can combine these codes with any separator characters - slashes (/), dashes (-), commas, spaces, or even words - to build any date format you need.

Changing the Format of a Single Date String

The simplest case is converting a single date string to a different format using pd.to_datetime() and strftime().

import pandas as pd

# Parse a date string into a datetime Series
date_sr = pd.to_datetime(pd.Series("2020-12-08"))

# Change format to DD/MM/YYYY
formatted = date_sr.dt.strftime('%d/%m/%Y')

print(formatted)

Output:

0    08/12/2020
dtype: object

The original 2020-12-08 (YYYY-MM-DD) is now displayed as 08/12/2020 (DD/MM/YYYY).

Formatting a Date Range Series

When working with a series of dates, strftime() applies the format to every element in the Series.

import pandas as pd

# Create a date range with monthly frequency
date_sr = pd.Series(pd.date_range('2019-12-31', periods=3, freq='M'))

# Assign custom index labels
date_sr.index = ['Day 1', 'Day 2', 'Day 3']

print("Original format:")
print(date_sr)
print()

# Change format to DD,MM,YYYY
formatted = date_sr.dt.strftime('%d,%m,%Y')

print("Formatted:")
print(formatted)

Output:

Original format:
Day 1 2019-12-31
Day 2 2020-01-31
Day 3 2020-02-29
dtype: datetime64[ns]

Formatted:
Day 1 31,12,2019
Day 2 31,01,2020
Day 3 29,02,2020
dtype: object

Common Date Format Examples

Below are several popular date formats you might need in real-world projects:

import pandas as pd

date_sr = pd.to_datetime(pd.Series("2024-07-15"))

# DD-MM-YYYY
print("DD-MM-YYYY: ", date_sr.dt.strftime('%d-%m-%Y').iloc[0])

# MM/DD/YYYY
print("MM/DD/YYYY: ", date_sr.dt.strftime('%m/%d/%Y').iloc[0])

# Full month name with day and year
print("Month DD, YYYY: ", date_sr.dt.strftime('%B %d, %Y').iloc[0])

# Abbreviated format with weekday
print("Day, Mon DD, YYYY:", date_sr.dt.strftime('%a, %b %d, %Y').iloc[0])

# Include time
print("With time: ", date_sr.dt.strftime('%d/%m/%Y %I:%M %p').iloc[0])

Output:

DD-MM-YYYY:        15-07-2024
MM/DD/YYYY: 07/15/2024
Month DD, YYYY: July 15, 2024
Day, Mon DD, YYYY: Mon, Jul 15, 2024
With time: 15/07/2024 12:00 AM

Formatting Datetime Columns in a DataFrame

In practice, you'll most often reformat a datetime column inside a DataFrame - for example, before exporting data to CSV or displaying it in a report.

import pandas as pd

df = pd.DataFrame({
'event': ['Launch', 'Update', 'Release'],
'date': ['2024-01-15', '2024-06-20', '2024-12-01']
})

# Convert the column to datetime
df['date'] = pd.to_datetime(df['date'])

# Add a formatted string column
df['date_formatted'] = df['date'].dt.strftime('%d %B %Y')

print(df)

Output:

     event       date    date_formatted
0 Launch 2024-01-15 15 January 2024
1 Update 2024-06-20 20 June 2024
2 Release 2024-12-01 01 December 2024
Important: strftime() Returns Strings, Not Datetimes

After applying strftime(), the resulting column has dtype: object (strings), not datetime64. This means you can no longer perform datetime arithmetic on it.

# This works: datetime column
print(df['date'] + pd.Timedelta(days=7))

# This raises an error: string column
print(df['date_formatted'] + pd.Timedelta(days=7)) # TypeError

Best practice: Keep the original datetime column for computations and create a separate formatted column for display purposes.

Formatting Dates with Timezone Information

If your datetime data includes timezone information, strftime() works the same way. You can also include the %Z directive to display the timezone name.

import pandas as pd

date_sr = pd.Series(pd.date_range(
'2024-01-01', periods=3, freq='M', tz='Asia/Calcutta'
))

formatted = date_sr.dt.strftime('%d-%m-%Y %H:%M %Z')

print(formatted)

Output:

0    31-01-2024 00:00 IST
1 29-02-2024 00:00 IST
2 31-03-2024 00:00 IST
dtype: object

Parsing Custom Formats with pd.to_datetime()

Sometimes your source data uses a non-standard date format. Use the format parameter of pd.to_datetime() to parse it correctly, then reformat as needed.

import pandas as pd

# Date strings in DD/MM/YYYY format
raw_dates = pd.Series(['25/12/2024', '01/07/2023', '15/03/2022'])

# Parse with explicit format
parsed = pd.to_datetime(raw_dates, format='%d/%m/%Y')

# Reformat to YYYY-MM-DD
reformatted = parsed.dt.strftime('%Y-%m-%d')

print(reformatted)

Output:

0    2024-12-25
1 2023-07-01
2 2022-03-15
dtype: object
note

Specifying the format parameter explicitly is faster and avoids ambiguity - for instance, pandas might otherwise interpret 01/07/2023 as January 7th instead of July 1st.

Conclusion

Changing the datetime format in pandas comes down to two key tools:

  • pd.to_datetime() to parse date strings into proper datetime objects.
  • strftime() to convert datetime objects into any string format you need.

By combining these with pandas' format directive codes (%Y, %m, %d, %H, etc.), you can transform dates into virtually any representation, from compact numeric formats like 15-07-2024 to verbose human-readable ones like Monday, July 15, 2024.

Just remember to keep your original datetime column intact for calculations and use the formatted version only for display.