Clean imported numeric columns
A column contains numbers stored as strings alongside malformed entries.
Pandas · Function
Converts scalar or sequence-like input to numeric data. Invalid values raise by default or can be converted to missing values with coercion; downcast and dtype_backend provide output-type control.
Pass a scalar, sequence, or Series to convert its values into numeric data. Parsing errors raise by default; choosing coercion turns invalid entries into missing values, while downcast can request a smaller compatible numeric dtype and dtype_backend can control nullable output representation.
pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=<no_default>)| Name | Type | Description |
|---|---|---|
arg | scalar, list, tuple, 1-d array, or Series | Value or collection to convert to numeric data; supported inputs include scalars, lists, tuples, one-dimensional arrays, and Series. |
errors | {'raise', 'coerce'}, default 'raise' | How invalid parsing is handled: raise an exception or convert invalid values to NaN. |
downcast | str, default None | Requests a smaller compatible numeric dtype; accepted categories are integer, signed, unsigned, and float. |
dtype_backend | {‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’ | Controls the nullable dtype backend used for the result. |
Numeric data after parsing. The return type depends on the input: a Series for Series input, otherwise an ndarray; with dtype_backend settings, nullable representations may be used.
import pandas as pd
values = pd.to_numeric(["10", "2.5", "unknown"], errors="coerce")
print(values)
# [10. 2.5 nan]Converts parseable values to numbers and represents the unparseable value as NaN.
A column contains numbers stored as strings alongside malformed entries.
A parsed column is larger than necessary for its value range.
A pipeline needs missing values instead of conversion failures.
The function converts a scalar, list, tuple, one-dimensional array, or Series to numeric data. Its default output is float64 or int64 for non-numeric input, depending on the values.
Follow-up: What happens to a Series? The result remains a Series; otherwise the result is an ndarray.
errors="raise" throws for invalid parsing, whereas errors="coerce" replaces invalid values with NaN. The default is "raise".
Follow-up: How can invalid values be handled without silently ignoring them? Catch the conversion exception explicitly rather than using the deprecated ignore option.
downcast accepts integer, signed, unsigned, or float and requests a smaller compatible dtype. It does not force an unsafe conversion when the values cannot fit.
Follow-up: What additional output choice is available? dtype_backend can select the nullable data representation, including nullable output for coerced values.
pandas.to_datetimepandas.to_timedeltapandas.DataFrame.astypepandas.api.types.is_numeric_dtype