Open In App

Change String To Date In Pandas Dataframe

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Working with date and time data in a Pandas DataFrame is common, but sometimes dates are stored as strings and need to be converted into proper date formats for analysis and visualization. In this article, we will explore multiple methods to convert string data to date format in a Pandas DataFrame.

Using pandas.to_datetime()

This is the most commonly used method to convert string dates into datetime64[ns] format. It automatically detects and converts various date formats, making it efficient for handling standard date strings.

Python
import pandas as pd

data = {
    'Date': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'Values': [100, 200, 300]
}

# Creating DataFrame
df = pd.DataFrame(data)

# Using pandas.to_datetime()
df['Date'] = pd.to_datetime(df['Date'])
print(df.dtypes)

Output
Date      datetime64[ns]
Values             int64
dtype: object

Explanation: This code creates a DataFrame from a dictionary with date strings and numerical values, then converts the 'Date' column to datetime64[ns] using pandas.to_datetime() for proper date handling.

Using datetime.strptime()

This method is useful when dealing with custom date formats. The strptime() function from Python’s datetime module allows precise control over how dates are parsed by specifying a format string.

Python
import pandas as pd
from datetime import datetime

data = {
    'Date': ['01-01-2022', '02-01-2022', '03-01-2022'],
    'Values': [100, 200, 300]
}

# Creating DataFrame
df = pd.DataFrame(data)

# Custom format conversion
df['Date'] = df['Date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y'))

print("\nAfter converting")
print(df.dtypes)

Output
After converting
Date      datetime64[ns]
Values             int64
dtype: object

Explanation: This code creates a DataFrame with date strings in DD-MM-YYYY format and numerical values. It uses apply() with datetime.strptime() to convert each date string into a datetime object for proper date handling.

Using pd.Series.dt.strftime()

Once a column is converted to a datetime64 type, strftime() helps format it into a specific date string format (e.g., DD-MM-YYYY). This method is useful for displaying dates in a customized way.

Python
import pandas as pd

data = {
    'Date': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'Values': [100, 200, 300]
}

# Creating DataFrame
df = pd.DataFrame(data)

# Convert Date column to datetime
df['Date'] = pd.to_datetime(df['Date'])

# Convert Date column to a different format
df['Formatted_Date'] = df['Date'].dt.strftime('%d-%m-%Y')

print(df)

Output
        Date  Values Formatted_Date
0 2022-01-01     100     01-01-2022
1 2022-01-02     200     02-01-2022
2 2022-01-03     300     03-01-2022

Explanation:The code converts date strings to datetime using pd.to_datetime() and formats them as DD-MM-YYYY using .dt.strftime(), storing the result in 'Formatted_Date'.

Using astype()

astype() method allows direct conversion of a column’s data type. When applied to a date column, it converts strings to datetime64[ns]. However, it requires that all values follow a uniform date format.

Python
import pandas as pd

data = {
    'Date': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'Values': [100, 200, 300]
}

# Creating DataFrame
df = pd.DataFrame(data)

# Using DataFrame.astype()
df['Date'] = df['Date'].astype('datetime64[ns]')

print("\nAfter converting")
print(df.dtypes)

Output
After converting
Date      datetime64[ns]
Values             int64
dtype: object

Explanation: This code creates a DataFrame with date strings and numerical values, then uses astype('datetime64[ns]') to convert the 'Date' column into a datetime format, enabling efficient date-based operations.

Handling different date formats in column

When a column contains multiple date formats, pandas.to_datetime() can be used with errors='coerce', which replaces invalid date formats with NaT (Not a Time). This ensures the dataset remains clean and properly formatted.

Python
import pandas as pd

data = {
    'Date': ['2022-01-01', '01-02-2022', 'March 3, 2022', 'Invalid Date'],
    'Values': [100, 200, 300, 400]
}

# Creating DataFrame
df = pd.DataFrame(data)

# Handling multiple date formats
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')

print("\nAfter converting")
print(df)

Output
After converting
        Date  Values
0 2022-01-01     100
1        NaT     200
2        NaT     300
3        NaT     400

Explanation: This code converts various date formats in the 'Date' column to datetime using pd.to_datetime(), handling errors with errors='coerce', which replaces invalid dates with NaT.


Next Article

Similar Reads