Pandas Datetime to String

Read Time:6 Minute, 27 Second

Table of Contents

Pandas is a Python toolkit for data analysis and manipulation that is simple, flexible, powerful, fast, and open-source. It is instrumental when dealing with large datasets for cleaning, analyzing, altering, and examining data.

The Python library for pandas allows programmers to examine enormous amounts of data and interpret or make statistical conclusions. It can quickly clean a large dataset to make it understandable, readable, and analyzeable. You can use it to create a relationship or detect a correlation between data. You can use it to conduct any mathematical operation on the data, such as sum, average, max, min, etc.

Pandas also have a data cleaning feature that allows you to eliminate undesirable or unnecessary data, NULL or empty data, and incorrect data from a dataset. You can use the pip pandas command to install it quickly. However, some Python distributions, such as Spyder and Anaconda, come with the pandas library preinstalled. As a result, if you’re using these distributors to write your code, you have to import the panda’s library into your program, and you’re ready to go.

You can now utilize the pandas’ library’s modules and functions in your program after you’ve imported the library. This tutorial will show you how to convert a DateTime to a string in Python using the Pandas package. We’ll show you how to convert DateTime to string in Python utilizing the panda’s module with some basic and easy-to-understand examples. So let’s get started.

Pandas Datetime to String

Using a date, time, or datetime object, the strftime() method returns a string representing the date and time.


Example 1: converting a datetime to a string with strftime()

The following application transforms a datetime object with the current date and time to several string formats.

What is the working of strftime()?

The format codes in the software, as mentioned above, are %Y, % m, % d, and so on. The strftime() method accepts one or more format codes as input and produces a formatted string. The datetime class is imported from the datetime module. The strftime() method can be accessed by objects of the datetime type.

The now variable has the datetime object holding the current date and time saved.

To create formatted strings, use the strftime() function.

The strftime() method accepts a string that may have several format codes.


Example 2: Using a timestamp to create a string

Example 3: The appropriate date and time for the locale

The locale’s suitable date and time representation are represented using % c, % x, and % X format codes.

List of Format Codes

All of the codes passed to the strftime() method are listed below.

Directive Meaning Example
%a The weekday’s name abbreviation is Tue, Wen, …
%A A complete name of the weekday. Thursday, Friday, …
%w a decimal number representing a weekday 0, 1, 2 …, 6
%d Months’ Day as a zero-padded decimal. 01, 02,03,04 …, 31
%-d Month’s Day as a decimal number. 1, 2, …, 30
%b Abbreviated month name. Jan, Feb, …, Dec
%B Full month name. January, February, …
%m Month as a zero-padded decimal number. 01, 02, …, 12
%-m Month as a decimal number. 1, 2,3 …, 12
%y The year with no century as a decimal number with no zero-padding. 00, 01, …, 99
%-y The year with no century as a decimal number. 0, 1, …, 99
%Y The year with century representation as a decimal number. 2015, 2022, etc.
%H Hour (24-hour clock) as a decimal number with zero-padding. 00, 01,02 …, 23
%-H Hour (24-hour clock) representation as a decimal number. 0, 1,2,3 …, 23
%I Hour (12-hour clock) representation as a decimal number with zero-padding. 01, 02, 03, …, 12
%-I Hour (12-hour clock) decimal number representation. 1, 2, 3,4 … 12
%p Locale’s AM or PM. AM, PM
%M Minute as a decimal number with zero-padding. 00, 01,02,03, …, 59
%-M Minute decimal number representation. 0, 1, 2, 3 …, 59
%S Second, as a zero-padded decimal number. 00, 01, 02, 03, …, 59
%-S Second as a decimal number. 0, 1, 2, 3 …, 59
%f Microsecond’s decimal number representation, with left zero-padding. 000000 – 999999
%z The form +HHMM or -HHMM UTC offset.
%Z represents the name of the Time zone.
%j year’s day as a decimal number with zero-padding. 001, 002, 003, …, 366
%-j year’s days as a decimal number. 1, 2,3,4, …, 366
%U Year’s week number (Sunday – weeks’ first day). All days in a new year before the first Sunday are considered in week 0. 00, 01, 02, 03, …, 53
%W Year’s week number (Week’s first day as Monday ). All days in a new year before the first Monday are considered in week 0. 00, 01, …, 53
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%% A literal ‘%’ character. %

Example: Convert DateTime to String in Pandas

Let’s say we have the pandas DataFrame below, which shows the sales of a store on four special days:

Example: Convert the DateTime to string

To convert the DateTime to string in this example, we’ll utilize the lambda and functions. Take a look at the following example command:

When you run the command listed above, you will see the following output:


Convert the DateTime to string

The result of the function is identical to that of the pandas.Series.dt.strftime() function, as you can see. As a result, using pandas in Python to convert a datetime to a string is simple.

Example: Convert DateTime to String

To convert DateTime to string, we use pandas.Series.dt.strftime() function in the next example. Here’s an example of code:

The following is the result of the code mentioned above:

Convert DateTime to String
Convert DateTime to String

If you look closely, you’ll notice that the data’s format or order has been altered, indicating that you can now enter the date in your preferred format.

Example: Using pd.to_datetime()

The pd.to_datetime() function is used in this example.


You’ll get the following output if you run this command:

using pd.to_datetime()
using pd.to_datetime()

Example: Convert a Pandas Series of datetime Objects to their String Equivalents

A Pandas Series refers to a one-dimensional array that can hold any data and labels. Let’s say you’ve got a pandas collection of datetime objects. Using the strftime() function and specific format codes, we can convert a datetime object to its string counterpart. However, converting the datetime objects of a pandas series requires a somewhat different approach. This section will discuss how such a conversion can be accomplished using examples.

Take a look at the code below. It starts by creating a pandas Series of datetime objects, transforming it into a pandas Series of string objects.

Take note of the output’s dtype value. The first indicates that the series comprises datetime objects, whereas the second suggests that it contains string objects.

The lambda function is also used to change the data type of objects. For more information, see the code below. The lambda function performs the conversion using the strftime() function.



In this article, we’ve seen three pandas routines in python used to convert DateTime to string:, pandas.Series.dt.strftime(), and pd.to_datetime(). We’ve supplied sample examples for each function to help you learn how to utilize them in your projects.


WP Ad Inserter plugin for WordPress