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 this datetime to a String format for example to MM/DD/YYYY (%m/%d/%Y) and to any other string date pattern. Changing the datetime format changes what order each date field is arranged and the style of the format. Details of the string format can be found in 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 default YYYY-MM-DD (%Y-%m-%d) to any custom string format.


# Below are quick example
# Convert datetype to string
df['ConvertedDate']=df['DateTypeCol'].astype(str)

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

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

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

# 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 date columns to string type
for col in  df.select_dtypes(include=['datetime64']).columns.tolist():
    df[col] = df[col].astype(str)

# 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 results. Our DataFrame contains column names Courses, Fee and InsertedDate.


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)

Yields below output. Note that on the above DataFrame example, I have used pandas.to_datetime() method to convert the date in string format to datetime type datetime64[ns]. Convert InsertedDate to DateTypeCol column.


   Courses    Fee InsertedDate DateTypeCol
0    Spark  22000   2021/11/24 2021-11-24
1  PySpark  25000   2021/11/25 2021-11-25
2   Hadoop  23000   2021/11/26 2021-11-26

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

First, let’s see how to convert the datetime (datetime64[ns]) column to String (object) type in pandas DataFrame. Use this approach If you wanted to convert 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 format %Y/%m/%d.


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

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


   Courses    Fee InsertedDate DateTypeCol ConvertedDate
0    Spark  22000   2021/11/24  2021-11-24    2021-11-24
1  PySpark  25000   2021/11/25  2021-11-25    2021-11-25
2   Hadoop  23000   2021/11/26  2021-11-26    2021-11-26

You can also try this. This converts the String date to datetime and back to a string. On below example, it converts the InsertDate (String type) values in 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(df)

Yields same output as above.

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

To convert default datetime (date) fromat to specific string format use pandas.Series.dt.strftime() method. This method takes the pattern format you wanted to convert to. Details of the string format can be found in 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(df)

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


   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 wanted to convert multiple date columns to String type, put all 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 wanted 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() and lambda function to change the datetime formate. Use the lambda function in the styling 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.


   Courses    Fee Duration  Discount InsertedDate
0    Spark  22000   30days      1000   2021/11/24
1  PySpark  25000   50days      2300   2021/11/25
2   Hadoop  23000   55days      1000   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)

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 list and all date columns from datetime to string type.

Happy Learning !!

You May Also Like

References

Pandas convert datetime string format

Leave a Reply

You are currently viewing Pandas Convert Date (datetime) to String Format