Types and Parsing
Coerce Bad Numbers
Parse a mixed list of numeric and non-numeric strings, converting invalid
entries to None rather than raising. By hand, wrap int(s) in a
try/except ValueError and append None on failure. With pandas,
pd.to_numeric(errors='coerce') turns every unparseable value into NaN
in one call.
By hand
With pandas
pd.to_numeric(df['x'], errors='coerce') attempts to parse each value;
strings that cannot be converted become NaN. The integer values are upcast
to float64 to accommodate NaN. The snapshot shows the raw float Series
(with nan positions), and result normalises NaN → None with
math.isnan for parity with the naive half.
naive.py
strings = ['12', 'x', '7', 'n/a', '28']
result = []
for s in strings:
try:
result.append(int(s))
except ValueError:
result.append(None)
print('RESULT:', result)
library.py
import math
import pandas as pd
from dalib.display import set_display
set_display()
strings = ['12', 'x', '7', 'n/a', '28']
df = pd.DataFrame({'x': strings})
s = pd.to_numeric(df['x'], errors='coerce')
result = [int(v) if not math.isnan(v) else None for v in s.tolist()]
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('values raw:', s.tolist())
print('RESULT:', result)
index: [0, 1, 2, 3, 4]
dtype: float64
values raw: [12.0, nan, 7.0, nan, 28.0]
RESULT: [12, None, 7, None, 28]
Implementation notes
errors='coerce'is the key switch:errors='raise'(default) would raise on the first invalid string, same asastype;errors='ignore'returns the original strings for unparseable entries.- The
float64upcast is the same NaN-forces-float pattern seen indiff-previous-rowandmerge-left— integer columns cannot storeNaN. - Use
pd.isna(s)ormath.isnan(v)to detect the coerced positions after the call. - Cross-reference:
cast-numeric-strings(this chapter) for the strictastypeapproach when all values are guaranteed clean.