0
0
Pandasdata~5 mins

to_datetime() for date parsing in Pandas

Choose your learning style9 modes available
Introduction

We use to_datetime() to change text or numbers into dates. This helps us work with dates easily in data.

You have a list of dates as text and want to analyze them as real dates.
You want to sort events by date but the dates are not in date format yet.
You need to calculate the difference between two dates stored as strings.
You want to filter data by a specific date or date range.
You want to add new columns like year or month from a date column.
Syntax
Pandas
pandas.to_datetime(arg, format=None, errors='raise', utc=None, dayfirst=False, yearfirst=False)

arg is the data to convert (like a list, Series, or string).

format helps speed up parsing if you know the date pattern (like '%Y-%m-%d').

Examples
Converts one date string to a pandas Timestamp object.
Pandas
import pandas as pd

# Convert a single date string
date = pd.to_datetime('2024-06-01')
print(date)
Converts a list of date strings into a pandas DatetimeIndex.
Pandas
dates = ['2024-06-01', '2024-07-15', '2024-08-20']
dates_parsed = pd.to_datetime(dates)
print(dates_parsed)
Uses dayfirst=True because dates are in day/month/year format.
Pandas
dates = ['01/06/2024', '15/07/2024']
dates_parsed = pd.to_datetime(dates, dayfirst=True)
print(dates_parsed)
Specifies the exact format to speed up parsing of dates without separators.
Pandas
dates = ['20240601', '20240715']
dates_parsed = pd.to_datetime(dates, format='%Y%m%d')
print(dates_parsed)
Sample Program

This code converts a column of date strings into real dates. Then it adds a new column showing the month number.

Pandas
import pandas as pd

# Sample data with dates as strings
data = {'event': ['start', 'middle', 'end'],
        'date_str': ['2024-06-01', '2024-07-15', '2024-08-20']}

df = pd.DataFrame(data)

# Convert the 'date_str' column to datetime
df['date'] = pd.to_datetime(df['date_str'])

# Add a new column for the month
df['month'] = df['date'].dt.month

print(df)
OutputSuccess
Important Notes

If a date string is invalid, errors='raise' will stop the program. Use errors='coerce' to get NaT (missing date) instead.

Using format speeds up conversion when you know the exact date pattern.

Remember to check if your dates use day-first or year-first order and set dayfirst or yearfirst accordingly.

Summary

to_datetime() changes text or numbers into date objects for easy date work.

You can convert single dates, lists, or whole columns in a DataFrame.

Use options like format and dayfirst to help pandas understand your dates correctly.