Pandas strftime Method Implementation with Examples

Pandas strftime Method Implementation with Examples

GET FREE AMZAON AUDIOBOOKS

Pandas Strftime allows you to modify the datetime into various formats. It is mostly used when you have datetime or date values and are not properly formatted.  In this entire tutorial, you will learn how to implement pandas strftime with various examples.

Syntax of the Pandas strftime

Series.dt.strftime(*args, **kwargs)

The input of the function will be the DateTime value and the output will be formatted date and time for the corresponding input.

Examples of Pandas Strftime

In this section, you will know all the examples of the strftime and how to use it on string and DateTime type of dates.

Example 1: Applying pandas strftime on dates of string type

Suppose you have a data frame consists of dates in the date column. But it is of string type. Then you can format it by first converting the date column to datetime using the pd.to_datetime() method. And then you will apply strftime to it. Execute the below lines of code.

import pandas as pd
df = pd.DataFrame({"dates":["03-01-2021","04-01-2021","05-01-2021","06-01-2021"]})
df["dates"] = pd.to_datetime(df["dates"])
df["formatter_dates"] = df["dates"].dt.strftime('%m/%d/%Y')
print(df)

Output

Applying pandas strftime on dates of string type
Applying strftime on dates of string type

Example 2: Applying pandas strftime on dates of datetime type

Let’s take another example. Here you have dates that are already of datetime type. You can easily format it with the strftime. For example, I want the dates to format as month/day/year. Then I will execute the below lines of code.

import pandas as pd
dates = pd.date_range("2020-10-20",periods=5)
df = pd.DataFrame({"dates":dates})
df["dates"] = pd.to_datetime(df["dates"])
df["formatter_dates"] = df["dates"].dt.strftime('%m/%d/%Y')
print(df)

Output

Applying pandas strftime on dates of datetime type
Applying strftime on dates of datetime type

Example 3: Applying strftime on dates of with time

You can also format the dates with time using strftime. Here you have to just add one format % r with the other formate. Execute the below lines of code.

import pandas as pd
dates = pd.date_range("2020-10-20 11:50",periods=5)
df = pd.DataFrame({"dates":dates})
df["dates"] = pd.to_datetime(df["dates"])
df["formatter_dates"] = df["dates"].dt.strftime('%m/%d/%Y %r')
print(df)

In the above code, you can see I have used %r with %m/%d/%Y. It will format the time also. Below is the output for the above code.

Output

Applying pandas strftime on dates with time
Applying strftime on dates with time

Conclusion

Pandas strftime allows you to easily format dates that are of string or a DateTime type. This method is very useful while you are dealing with time-series data like stocks market, forex, and other financial data. These are the basic example I have compiled for you. I hope you have liked this tutorial. If you have any doubt then you contact us for more help.

Source:

Pandas Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner