• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Pandas Convert Date (datetime) to String Format

Pandas Convert Date to String Format – To change/convert the Pandas datetime (datetime64[ns]) from default format to String/Object or custom format use pandas.Series.dt.strftime() method. By default Pandas datetime format is YYYY-MM-DD (%Y-%m-%d).

In this article, I will explain how to convert a column of datetime values to a String format using the strftime method. Changing the datetime format modifies the arrangement and style of each date field. Details of the string format can be found in the Python string format doc.

Also, here I will cover how to change/convert the string to date format by using pandas.to_datetime() methods. You can also use DataFrame.style.format() and lambda function.

1. Quick Examples of Convert Datetime to String Format in Pandas

If you are in a hurry, below are some quick examples of how to convert the datetime (date) format from the default YYYY-MM-DD (%Y-%m-%d) to any custom string format.


# Below are some quick examples

# Example 1: Convert datetype to string
df['ConvertedDate']=df['DateTypeCol'].astype(str)

# Example 2: Using to_datetime() & astype()
df['ConvertedDate']=pd.to_datetime(df['DateTypeCol'].astype(str), format='%Y/%m/%d')

# Example 3: Conver DataTime to Different Format
df['ConvertedDate'] = df['DateTypeCol'].dt.strftime('%m/%d/%Y')

# Example 4: Using DataFrame.style.format() and lambda function
df.style.format({"DateTypeCol": lambda t: t.strftime("%d/%m/%Y")})

# Example 5: Convert multiple date columns to string type
date_columns = ["date_col1","date_col2","date_col3"]
df[date_columns] = df[date_columns].astype(str)

# Example 6: Convert all date columns to string type
for col in  df.select_dtypes(include=['datetime64']).columns.tolist():
    df[col] = df[col].astype(str)

# Example 7: Convert all date columns to string type
date_columns = df.select_dtypes(include=['datetime64']).columns.tolist()
df[date_columns] = df[date_columns].astype(str)

Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate the results. Our DataFrame contains column names Courses, Fee and InsertedDate.


# Create DataFrame
import pandas as pd
technologies = ({
    'Courses':["Spark","PySpark","Hadoop"],
    'Fee' :[22000,25000,23000],
    'InsertedDate':["2021/11/24","2021/11/25","2021/11/26"]
               })
df = pd.DataFrame(technologies)
print("Create Dataframe:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)

Yields below output.

Pandas convert datetime String

You can use Pandas.to_datetime() method to convert the date in string format to datetime type datetime64[ns]. Convert InsertedDate to DateTypeCol column.


# Use pandas.to_datetime() to change datetime format
df['DateTypeCol'] = pd.to_datetime(df.InsertedDate)
print("Convert object to datetime:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)

Yields below output.

Pandas convert datetime String

2. Use astype() to Change datetime to String Format

First, let’s see how to convert the datetime (datetime64[ns]) column to the String (object) type in Pandas DataFrame. Use this approach to convert the date to String type as-is without changing the format. You can use this if the date is already in the format you want it in string form. The below example returns the date as a string with the format %Y/%m/%d.


# Convert datetime to string/object
df['ConvertedDate']=df['DateTypeCol'].astype(str)
print("After converting datetime to string format:\n", df)
print("----------------------------------")
print("Type of the columns:\n", df.dtypes)

dtype of column ConvertedDate will be object (string). Yields below output.

Pandas convert datetime String

You can also try this. This converts the String date to datetime and back to a string. Below example, it converts the InsertDate (String type) values into the format %Y/%m/%d to ConvertedDate with format %Y-%m-%d


# Convert datetime from datetime64[ns] to string type
df['ConvertedDate']=pd.to_datetime(df['InsertedDate'].astype(str), format='%Y/%m/%d')
print("After converting datatime to string format:\n", df)

Yields the same output as above.

3. Use pandas.Series.dt.strftime() to Convert datetime Column Format

To convert the default datetime (date) format to a specific string format use pandas.Series.dt.strftime() method. This method takes the pattern format you want to convert. Details of the string format can be found in the Python string format doc.

Note: strftime stands for String From Time.


# Change in datetime format to other format
df['ConvertedDate'] = df['DateTypeCol'].dt.strftime('%m/%d/%Y')
print("After converting datatime to string format:\n", df)

Yields below output. This example changes the DateTypeCol (datetime) into MM/DD/YYYY format and stores into ConvertedDate column.


# Output:
# After converting datatime to string format:
   Courses    Fee InsertedDate DateTypeCol ConvertedDate
0    Spark  22000   2021/11/24  2021-11-24    11/24/2021
1  PySpark  25000   2021/11/25  2021-11-25    11/25/2021
2   Hadoop  23000   2021/11/26  2021-11-26    11/26/2021

4. Convert Multiple DataFrame Columns from Datetime to String

If you want to convert multiple date columns to String type, put all the date column names into a list and use it with astype().


# Convert multiple date columns to string type
date_columns = ["date_col1","date_col2","date_col3"]
df[date_columns] = df[date_columns].astype(str)

5. Convert All Datetime columns to String Type

If you have more than one date column in the same format and want to convert to a specific format use the following approach


# Convert all date columns to string type
for col in  df.select_dtypes(include=['datetime64']).columns.tolist():
    df[col] = df[col].astype(str)

Alternatively, you can also try.


# Convert all date columns to string type
date_columns = df.select_dtypes(include=['datetime64']).columns.tolist()
df[date_columns] = df[date_columns].astype(str)

6. Use DataFrame.style.format() and Lambda Function to Change datetime Format

You can also use the DataFrame.style.format() with lambda function to change the datetime format. Use the lambda function in the string it as mm/dd/yyyy.


# Using DataFrame.style.format() and lambda function
df.style.format({"InsertedDate": lambda t: t.strftime("%m/%d/%Y")})
print(df)

Yields below output.


# Output:
Create Dataframe:
    Courses    Fee InsertedDate
0    Spark  22000   2021/11/24
1  PySpark  25000   2021/11/25
2   Hadoop  23000   2021/11/26

7. Complete Example For Change the datetime Format


# Create a DataFrame.

import pandas as pd
technologies = ({
    'Courses':["Spark","PySpark","Hadoop"],
    'Fee' :[22000,25000,23000],
    'InsertedDate':["2021/11/24","2021/11/25","2021/11/26"]
               })
df = pd.DataFrame(technologies)
# Use pandas.to_datetime() to change datetime format
df['DateTypeCol'] = pd.to_datetime(df.InsertedDate)
print(df)

# Convert datetype to string
df['ConvertedDate']=df['DateTypeCol'].astype(str)
print(df)

# Using to_datetime()  & astype()
df['ConvertedDate']=pd.to_datetime(df['DateTypeCol'].astype(str), format='%Y/%m/%d')
print(df)

# change in date time format
df['ConvertedDate'] = df['DateTypeCol'].dt.strftime('%m/%d/%Y')
print(df)

# Using DataFrame.style.format() and lambda function
df.style.format({"DateTypeCol": lambda t: t.strftime("%d/%m/%Y")})
print(df)

# Convert all date columns to string type
date_columns = df.select_dtypes(include=['datetime64']).columns.tolist()
df[date_columns] = df[date_columns].astype(str)

Frequently Asked Questions on Pandas Convert Date (datetime) to String

How can I convert a specific datetime column to string format in a Pandas DataFrame?

You can convert a specific datetime (date) column to a string format using pandas.Series.dt.strftime() the method. For example, df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')

How can I include the day of the week in the string representation of a datetime column?

You can include the day of the week using %A (full weekday name) or %a (abbreviated weekday name). For example, df['Date'] = df['Date'].dt.strftime('%A, %Y-%m-%d')

How do I handle missing values in the datetime column when converting to string format?

If there are missing values in the datetime column, you should handle them before converting to string format. You can use fillna to replace missing values with a default datetime or a specific string.
For example, df['Date'].fillna('Unknown', inplace=True)<br/>df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')

How can I convert the entire DataFrame with multiple datetime columns to string format?

You can convert multiple datetime columns in a DataFrame to string format by applying the strftime() method to each column.

Conclusion

In this article, you have learned how to change the datetime formate to string/object in pandas using pandas.to_datetime(), pandas.Series.dt.strftime(), DataFrame.style.format(), and lambda function with examples also learn how to change multiple selected columns from the list and all date columns from datetime to string type.

Happy Learning !!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply