Adventures in Machine Learning

Mastering Datetime Manipulation with Pandas: Create and Modify Formats

Pandas: Mastering Datetime Manipulation

Pandas is a powerful Python library renowned for its comprehensive toolkit for working with datasets. It excels in data organization, cleaning, modification, and analysis, making it a favorite among data analysts and scientists.

Changing datetime format using Pandas strftime function:

Pandas employs the YYYY-MM-DD format for datetime values by default.

However, situations arise where you need to change the datetime format to suit your specific needs. Pandas’s strftime function provides a solution for converting datetime formats to a variety of options.

To utilize the strftime function, you must first install and import the Pandas library. Once you have these set up, you can employ the strftime function to modify the format of your datetime values.

For instance, if you wish to change the format from YYYY-MM-DD to DD-MM-YYYY, you can use:


This will generate a new column containing the datetime values in the DD-MM-YYYY format. Similarly, to change the format to DD-Month-YYYY, you can use:


In this case, the %B code represents the month name.

Lastly, if you want to alter the time format from HH:MM:SS to SS:MM:HH, you can use:


This will create a new column with the time values in the desired format.

Benefits of using Pandas package for working with datasets:

One of the key advantages of using the Pandas package for working with datasets is its versatility.

It supports a wide array of data types, including numerical, categorical, and datetime data. This makes it effortless to work with a variety of datasets without encountering data type compatibility issues.

Another notable benefit of using Pandas is its flexible data manipulation capabilities. It offers a wealth of functions for organizing, cleaning, and modifying data, providing the flexibility needed to handle nearly any dataset.

For example, you can leverage Pandas to filter out missing or duplicate data, replace values, and perform calculations on columns. Pandas also simplifies data analysis through a range of functions such as grouping, filtering, and aggregation.

These capabilities empower you to gain insights into your data and extract meaningful information. Finally, Pandas seamlessly integrates with other Python libraries such as Matplotlib and Seaborn for data visualization.

This integration allows you to create visually appealing graphs and charts that are easy to understand and share with others.

Pandas datetime and its data type:

Beyond its support for various data types, Pandas features a specific data type for datetime values.

This data type is called datetime64 and provides a set of functions for handling datetime values. One of the key features of the datetime64 data type is its support for time zones.

This enables you to work with datetime values in different time zones and seamlessly convert between them as required. Another valuable feature of the datetime64 data type is its support for datetime arithmetic.

You can perform addition and subtraction operations on datetime values using standard math operators, simplifying the calculation of time differences and durations.

Creating and modifying datetime format in Pandas:

Pandas is a versatile Python library that provides numerous features for working with datetime values.

It offers functions for creating, modifying, and analyzing datetime data, making it an excellent tool for data analysts and scientists. In this section, we will delve into how to create and modify datetime values in Pandas.

Creating datetime values using Pandas to_datetime function:

The to_datetime function in Pandas serves as a powerful tool for creating datetime values from strings or numeric data. It can also be used to convert values between different datetime formats.

Consider the following code snippet as an example:

datestrings = ['2022-01-01', '2022-01-02', '2022-01-03']
dates = pd.to_datetime(datestrings)

In this example, we create a list of date strings and then utilize the to_datetime function to convert them into datetime values. The resulting dates variable will be a Pandas Series object containing the datetime values.

We can then utilize various functions within Pandas to further manipulate these values.

Using strftime to modify datetime format in Pandas:

The strftime function in Pandas allows you to modify datetime formats based on your preferences.

It accepts a format string as its argument, which specifies how the datetime values should be formatted. Here’s an example of how to use strftime:

dates_formatted = dates.dt.strftime('%d/%m/%Y')

In this example, we use the dt accessor to access the datetime properties and then apply the strftime function to convert the datetime values to a new format.

In this specific case, we are using the format string ‘%d/%m/%Y’ to format the values as day/month/year.

Changing format from YYYY-MM-DD to preferred format:

As demonstrated in the previous example, we can leverage strftime to modify datetime formats.

To change the format from the default YYYY-MM-DD to another preferred format, we need to specify the format string accordingly using strftime. For example, to change the format to DD-MMM-YYYY, we can use the following code:

dates_formatted = dates.dt.strftime('%d-%b-%Y')

In this code, we use the ‘%b’ code to represent the three-letter abbreviation of the month name.

This will create a new series with values in the DD-MMM-YYYY format.

Changing format to month name instead of number:

By default, Pandas displays the month number (e.g., 01 for January) in datetime values.

If we desire to display the month name instead, we can employ the strftime function and specify the ‘%B’ code in the format string. Here’s an example:

dates_formatted = dates.dt.strftime('%d %B %Y')

This will generate a new series with values in the format ‘DD MonthName YYYY’, where MonthName represents the full name of the month.

Changing time format in Pandas datetime:

Beyond modifying date formats, we can also alter time formats in Pandas datetime values. We can utilize strftime with appropriate format codes to modify the time format as well.

For instance, to change the time format from HH:MM:SS to HH:MM, we can use the following code:

times_formatted = dates.dt.strftime('%H:%M')

This will create a new series with values in the format ‘HH:MM’, where HH represents the hour in 24-hour format (00-23) and MM represents the minute (00-59).


Pandas is a robust and versatile Python library that provides a wide range of functions for working with datetime values.

We can create datetime values using the to_datetime function and modify the format of datetime values using the strftime function. These functions allow us to customize datetime values based on our preferences and requirements.

Whether it’s changing the date format or the time format, Pandas offers extensive flexibility to modify datetime values in various ways. By mastering these functions, data analysts and scientists can gain a deeper understanding of data and extract valuable insights from it.

In conclusion, Pandas is a powerful Python library that provides numerous functions for creating, modifying, and analyzing datetime values. By using the to_datetime and strftime functions, we can create datetime values, change the date and time formats, and customize the display of datetime data.

Pandas offers extensive flexibility and support for multiple data types, making it an essential tool for data analysts and scientists. The ability to work with and modify datetime data forms an integral part of modern data analysis, and mastering these functions can help analysts extract valuable insights from datasets.

By utilizing the functions of Pandas, data analysts can make informed decisions and gain a deeper understanding of the data, making it an indispensable tool in the world of data science.

Popular Posts