What is the to_numeric function in pandas?

The to_numeric function in pandas is used to convert the given argument to a numeric type. The argument is converted to float64 or int64 by default, based on what is provided in the arguments. We can use the downcast parameter if we want to convert data to a particular type.

The illustration below shows how the to_numeric function works:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Syntax

The syntax of the to_numeric function is as follows:

pd.to_numeric(arg, errors, downcast)

Parameters

The to_numeric function has the following parameters:

Parameter Description
arg Argument to be converted. Can be scalar, list, tuple, 1-d array, or Series.
errors Determines how to handle errors during conversions. Can be ignore, raise or coerce. It is raise by default.
downcast Specifies conversion to a particular datatype. Can be int, signed, unsigned or float. By default, it is None. If it is not None, pandas will downcast the data to the smallest data type possible.

Only the arg parameter is compulsory. The rest are optional.

Return value

Data in the converted format is returns based on what is passed in the argument.

Handling errors

Different types of error handling are possible within the to_numeric function:

  • If the error argument is passed as raise, then invalid parsing will raise an exception.

  • If the error argument is passed as coerce, then invalid parsing will be set as NaN.

  • If the error argument is passed as ignore, then invalid parsing will return the input.

Example

The code snippet below shows how we can use the to_numeric function in pandas:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved