bhylu.com

Advertisements

Pandas Numeric Conversion

to_numeric can reject or coerce invalid text. Coercion is useful for identifying bad rows when the original values remain available for review.

How it works

A source column may contain valid numeric strings, unit suffixes or accidental words. Removing every non-digit character is unsafe because signs, decimal points and units can change meaning. Parse according to the known source format, then separate failed conversions from genuinely missing input. This example deliberately retains the bad string rather than silently replacing it with a plausible quantity.

Run the code

Select Try in playground to load the complete editable source. Nothing executes until you choose Run. The browser uses Pyodide 0.27.7, Pandas 2.2.3 and NumPy 2.0.2. Each ordinary execution starts a fresh isolated runtime. The tables are fictional and do not use personal files.

For local work, use a separate virtual environment and run python numeric-parsing.py. Download the complete source. The reviewed local environment uses Python 3.12.14, Pandas 3.0.6 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with Pandas 2.2.3 and NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

numeric-parsing.py

import pandas as pd
import numpy as np

raw = pd.Series(["12.5", "8", "heavy"], dtype="string")
parsed = pd.to_numeric(raw, errors="coerce")
rejected = raw[parsed.isna() & raw.notna()]
print(parsed.dropna().astype("float64").tolist())
print(rejected.tolist())

Expected result

[12.5, 8.0]
['heavy']

Two values parse and one original token is flagged for correction. A coercion-produced missing value is not evidence that the sender originally left the field blank. Keep enough context to distinguish those cases.

Use Pandas’s official reference for precise contracts, supported versions and numerical limitations.