Pandas Convert Date (Datetime) To String Format
Last Updated :
12 Jun, 2025
We are given a column containing datetime values in a pandas DataFrame and our task is to convert these datetime objects into string format. This conversion is useful for visualizing data, exporting it to external applications, or performing string operations. For example, if we have this input:
a = {
'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']),
'x': [101, 201, 301],
'y': [909, 1809, 2709]
}
Then the output after conversion to strings might look like:
dt x y
0 01-01-2024 101 909
1 02-01-2024 201 1809
2 03-01-2024 301 2709
Using astype(str)
The astype(str) method converts the datetime column directly into strings. This approach is simple but does not allow formatting customization.
Python
import pandas as pd
a = {
'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']),
'x': [101, 201, 301],
'y': [909, 1809, 2709]
}
df = pd.DataFrame(a)
print(type(df['dt'][0]))
df['dt'] = df['dt'].astype(str)
print(type(df['dt'][0]))
Output<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<type 'str'>
Explanation:
- astype(str) changes the entire column dt from datetime to string type.
- type() checks the datatype before and after conversion.
Using strftime()
The strftime() method formats each datetime value into a string based on the format you specify.
Python
import pandas as pd
a = {
'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']),
'x': [101, 201, 301],
'y': [909, 1809, 2709]
}
df = pd.DataFrame(a)
df['dt'] = df['dt'].dt.strftime('%d-%m-%Y')
print(df)
df.info()
Output:
dt x y
0 01-01-2024 101 909
1 02-01-2024 201 1809
2 03-01-2024 301 2709
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 dt 3 non-null object
1 x 3 non-null int64
2 y 3 non-null int64
Explanation:
- strftime('%d-%m-%Y') converts each date to a custom string format.
- The dt accessor is used to apply string formatting on datetime columns.
Using apply() with lambda
You can convert datetime to string using the apply() function with a lambda expression. This offers flexibility to apply custom logic during conversion.
Python
import pandas as pd
a = {
'dt': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03']),
'x': [101, 201, 301],
'y': [909, 1809, 2709]
}
df = pd.DataFrame(a)
df['dt'] = df['dt'].apply(lambda d: d.strftime('%B %d, %Y'))
print(df)
Output dt x y
0 January 01, 2024 101 909
1 January 02, 2024 201 1809
2 January 03, 2024 301 2709
Explanation:
- .apply(lambda d: d.strftime(...)) allows full customization of the date format.
- You can use any valid format inside strftime — e.g., full month name with %B.
Similar Reads
How to Convert Datetime to Date in Pandas ? DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read
How to Convert String to Date or Datetime in Polars When working with data, particularly in CSV files or databases, it's common to find dates stored as strings. If we're using Polars, a fast and efficient DataFrame library written in Rust (with Python bindings), we'll often need to convert these strings into actual date or datetime objects for easier
5 min read
Convert column type from string to datetime format in Pandas dataframe To perform time-series operations, dates should be in the correct format. Let's learn how to convert a Pandas DataFrame column of strings to datetime format. Pandas Convert Column To DateTime using pd.to_datetime()pd.to_datetime() function in Pandas is the most effective way to handle this conversio
4 min read
How to Convert Integer to Datetime in Pandas DataFrame? Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of  pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check
2 min read
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read