Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

pd.to_numeric

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.

How it works

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.

Syntax

pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=<no_default>)

Parameters

NameTypeDescription
argscalar, list, tuple, 1-d array, or SeriesValue 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.
downcaststr, default NoneRequests 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.

Returns

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.

Key points

  • Use the function to parse input values into numeric data.
  • Without downcasting, non-numeric input generally produces float64 or int64 according to the parsed data.
  • Set downcast when a smaller integer, unsigned integer, signed integer, or floating-point dtype is desirable.
  • The argument may be a scalar, sequence, one-dimensional array, or Series.
  • Coercion converts unparseable entries into NaN instead of stopping the operation.
  • The output type follows the input category rather than always being a Series.

Common mistakes

  • The default error policy raises when any value cannot be parsed; use errors="coerce" when invalid values should become missing.
  • Very large numbers may lose precision during conversion.

Example: Convert mixed values safely

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.

When to use it

Clean imported numeric columns

A column contains numbers stored as strings alongside malformed entries.

Reduce numeric storage

A parsed column is larger than necessary for its value range.

Continue after invalid parsing

A pipeline needs missing values instead of conversion failures.

Interview questions

QWhat does pandas.to_numeric do, and what does it return?

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.

QHow do you handle values that cannot be parsed?

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.

QWhat is the purpose of downcast and dtype_backend?

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_datetime
  • pandas.to_timedelta
  • pandas.DataFrame.astype
  • pandas.api.types.is_numeric_dtype

Source: pandas.to_numeric — pandas documentation. Checked 2026-10-15.