Fundamentals 8 min read

Master Converting String Dates to Datetime in Pandas with pd.to_datetime

This guide shows how to transform string‑typed date columns into proper datetime objects using pandas' pd.to_datetime, covering basic usage, key parameters, handling various formats, timestamps, and practical code examples for reliable data cleaning.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Converting String Dates to Datetime in Pandas with pd.to_datetime

Data Requirement

The task is to convert a column containing date values represented as strings, such as 2021-11-27 00:00:00, into proper datetime types.

Simple Solution

In most cases you can directly apply pd.to_datetime(df['date']) without specifying a format; pandas will infer the appropriate datetime type automatically.

import pandas as pd

df = ...

# Show original string values
print(df['date'].values)

# Convert to datetime
converted = pd.to_datetime(df['date'])
print(converted)

Key Parameters of pd.to_datetime

errors : Determines how conversion errors are handled. Options are raise (default, stops execution), coerce (invalid parsing becomes pd.NaT), and ignore (returns the original input).

format : Explicit date format string (e.g., %Y-%m-%d) used when pandas cannot infer the format automatically.

infer_datetime_format : When set to True, pandas uses the first successfully parsed format as a template for the rest, often speeding up conversion by 5‑10× for consistent formats.

unit : Specifies the time unit of integer timestamps (default ns). Common units are ms, s, and D.

origin : Reference point for numeric timestamps. The default unix corresponds to 1970‑01‑01 00:00:00 UTC; a custom origin can be provided as a timestamp.

Examples of Various Formats

pd.to_datetime(['03Apr2022', '04-Apr-2022', '2022-01-01', '20220101', '2022/01/01', 'Oct 10 2021'])

When the format is ambiguous, you can supply a format string:

# Day‑Month‑Year
pd.to_datetime(['010122'], format='%d%m%y')

# Month‑Day‑Year
pd.to_datetime(['020122'], format='%m%d%y')

# Year‑Month‑Day (two‑digit year)
pd.to_datetime(['220301'], format='%y%m%d')

Timestamp Conversion

# Milliseconds
pd.to_datetime([1648195805123], unit='ms')

# Seconds
pd.to_datetime([1648195805], unit='s')

# Days since epoch
pd.to_datetime([19076], unit='D')

Custom origins adjust the base date for numeric timestamps:

pd.to_datetime([1, 2, 3], unit='D', origin=pd.Timestamp('1960-01-01'))

Summary

Understanding and using pd.to_datetime simplifies date handling in pandas, leveraging the underlying datetime library while offering flexible parameters for error handling, format specification, performance optimization, and timestamp conversion, making data cleaning more robust and efficient.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondatetimedata cleaningpandaspd.to_datetime
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.