• Post author:
  • Post category:Pandas
  • Post last modified:October 2, 2024
  • Reading time:18 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).

Advertisements

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.

Key Points –

  • Utilize the strftime() method to convert datetime objects to string format.
  • Use methods like astype(), strftime(), or pd.to_datetime() to convert datetime to string.
  • Understand the common format codes (e.g., %Y, %m, %d, %H, %M, %S) used in strftime() for date and time representation.
  • Convert the entire DateTimeIndex to strings using the strftime() method for index data.
  • Apply the strftime() method directly on Pandas Series objects containing datetime data.

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

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.

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

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)

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)

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

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

Leave a Reply