The Wayback Machine - https://web.archive.org/web/20250113235042/https://www.geeksforgeeks.org/pandas-series-dt-strftime/
Open In App

Pandas Series dt.strftime() Method | Change Date Format in Series

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The dt.strftime() method converts the datetime objects in the Pandas Series to a specified date format.

The function returns an index of formatted strings specified by date_format, which supports the same string format as the Python standard library.

Example

Python
import pandas as pd 
sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-02-2 10:30', 
            '2010-1-1 09:25', '2019-12-31 00:00']) 
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] 
sr.index = idx 
sr = pd.to_datetime(sr) 
result = sr.dt.strftime('% B % d, % Y, % r')
print(result)

Output:

dt.strftime output example

Syntax

Syntax: Series.dt.strftime(Date_Format) 

Parameter 

  • date_format : Date format string (e.g. “%Y-%m-%d”) 

Returns: NumPy ndarray of formatted string

How to change the Date Format of DateTime objects in a Pandas Series

To change the date format of DateTime objects in a Pandas Series we use the dt.strftime method of the Pandas library in Python.

To understand it better, let us look at some example

Example:

Python
# importing pandas as pd
import pandas as pd

# Creating the Series
sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'M',
                            tz = 'Asia / Calcutta'))

# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']

# set the index
sr.index = idx

# Print the series
print(sr)

Output:

datetime series created

Now we will use the Series dt.strftime() function to convert the dates in the series object to the specified format.

Python
# convert to the given date format
result = sr.dt.strftime('% d % m % Y, % r')

# print the result
print(result)

Output :

date format changed after dt.strftime

As we can see in the output, the dt.strftime() function has successfully converted the dates in the given series object to the specified format.

Pandas Series dt.strftime() Method | Change Date Format in Series – FAQs

How to Convert Date Format in strftime in Python?

The strftime method is used to format date objects as strings according to a specified format. Here’s an example:

from datetime import datetime

# Current date
now = datetime.now()

# Convert to string with specific format
formatted_date = now.strftime('%d-%m-%Y %H:%M:%S')

print(formatted_date)

How to Change Date Format from yyyy-mm-dd to mm-dd-yyyy in Pandas

To change the date format from yyyy-mm-dd to mm-dd-yyyy in a Pandas DataFrame, you can use the dt.strftime method. Here’s an example:

import pandas as pd

# Sample DataFrame
data = {'date': pd.to_datetime(['2023-07-12', '2023-07-13', '2023-07-14'])}
df = pd.DataFrame(data)

# Change format to mm-dd-yyyy
df['formatted_date'] = df['date'].dt.strftime('%m-%d-%Y')

print(df)

How to Change String Date Format in Pandas

To change the format of a string date in a Pandas DataFrame, you can first convert the string to a datetime object and then reformat it. Here’s an example:

import pandas as pd

# Sample DataFrame with string dates
data = {'date_str': ['2023-07-12', '2023-07-13', '2023-07-14']}
df = pd.DataFrame(data)

# Convert string to datetime and then reformat
df['formatted_date'] = pd.to_datetime(df['date_str']).dt.strftime('%m-%d-%Y')

print(df)

How to Convert String yyyy-MM-dd to Date Format in Python

To convert a string in yyyy-MM-dd format to a date object in Python, you can use the datetime.strptime method. Here’s an example:

from datetime import datetime

# Sample date string
date_str = '2023-07-12'

# Convert string to date object
date_obj = datetime.strptime(date_str, '%Y-%m-%d')

print(date_obj)

How to Use strftime in Pandas DataFrame

You can use the dt.strftime method to apply strftime formatting to a datetime column in a Pandas DataFrame. Here’s an example:

import pandas as pd

# Sample DataFrame with datetime column
data = {'date': pd.to_datetime(['2023-07-12', '2023-07-13', '2023-07-14'])}
df = pd.DataFrame(data)

# Use strftime to format datetime column
df['formatted_date'] = df['date'].dt.strftime('%d-%m-%Y %H:%M:%S')

print(df)

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article

Similar Reads

three90RightbarBannerImg