๐Ÿ€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

Pandas errors='coerce' โ€” What It Does + How to Use It

Answer

The errors='coerce' parameter tells pandas to convert invalid values to NaN (or NaT for dates) instead of raising an error. Use it with pd.to_numeric(), pd.to_datetime(), or astype() when your data has messy values you want to skip rather than fail on.

Why This Happens

Real-world data is dirty. Columns that should be numeric contain strings like "N/A", "missing", or "$100". Without errors='coerce', these values crash your conversion. With it, pandas quietly converts them to NaN so you can clean them up later.

Solution

The rule: when converting types on messy data, always use errors='coerce' first, then handle the resulting NaN values explicitly.

import pandas as pd

df = pd.DataFrame({'price': ['100', '200', 'N/A', 'unknown', '500']})

# โŒ Without coerce: fails on invalid values
df['price'] = pd.to_numeric(df['price'])
# ValueError: Unable to parse string "N/A"

# โœ… With coerce: invalid values become NaN
df['price'] = pd.to_numeric(df['price'], errors='coerce')
# Result: [100.0, 200.0, NaN, NaN, 500.0]

# โœ… Same pattern for dates
df = pd.DataFrame({'date': ['2020-01-01', 'not a date', '2020-03-01']})
df['date'] = pd.to_datetime(df['date'], errors='coerce')
# Result: [2020-01-01, NaT, 2020-03-01]

# โœ… Then handle the NaNs
df['price'] = df['price'].fillna(0)  # or dropna(), or interpolate()

Better Workflow

Zerve shows NaN counts and dtype summaries inline, so after using errors='coerce' you can immediately see how many values failed conversion and decide how to handle them.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes