๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

How to Convert Column to Datetime in Pandas - With Examples

Answer

Use pd.to_datetime() to convert a column to datetime. It handles most formats automatically, but for non-standard formats use the format parameter. Use errors='coerce' to convert unparseable values to NaT instead of failing.

Why This Happens

Date columns often import as strings. You need proper datetime types to do time-based operations: filtering by date range, extracting month/year, calculating time differences, or plotting time series. String dates don't support any of this.

Solution

The rule: use pd.to_datetime() for conversion, specify format for non-standard dates, always use errors='coerce' on messy data.

import pandas as pd

df = pd.DataFrame({'date': ['2024-01-15', '2024-02-20', '2024-03-25']})

# โœ… Basic conversion (auto-detects format)
df['date'] = pd.to_datetime(df['date'])

# โœ… Specify format for faster parsing and non-standard formats
df = pd.DataFrame({'date': ['15/01/2024', '20/02/2024', '25/03/2024']})
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')

# โœ… Handle invalid dates with coerce
df = pd.DataFrame({'date': ['2024-01-15', 'not a date', '2024-03-25']})
df['date'] = pd.to_datetime(df['date'], errors='coerce')  # bad value -> NaT

# โœ… Common format codes
# %Y = 4-digit year, %y = 2-digit year
# %m = month (01-12), %d = day (01-31)
# %H = hour (00-23), %M = minute, %S = second
pd.to_datetime('15-Jan-2024', format='%d-%b-%Y')

# โœ… After conversion, you can extract parts
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day_of_week'] = df['date'].dt.day_name()

Better Workflow

Zerve shows column dtypes inline, so you can immediately verify your datetime conversion worked and spot columns still stuck as object type.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes