What is the to_numeric function in pandas?

Share

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:

How does to_numeric function work

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:

import pandas as pd
import numpy as np
s = pd.Series(['1.0', '2', -3])
print("Series in string")
print(s)
print("Default Numeric Form")
print(pd.to_numeric(s))
print('\n')
print("Numeric Form using downcast = signed")
print(pd.to_numeric(s, downcast='signed'))
print('\n')
print("Numeric Form using downcast = int")
print(pd.to_numeric(s, downcast='integer'))
print('\n')
print("Ignoring errors")
s2 = pd.Series(['apple', '1.0', '2', -3])
print(pd.to_numeric(s2, errors='ignore'))
print('\n')
print("Corerce errors")
s2 = pd.Series(['apple', '1.0', '2', -3])
print(pd.to_numeric(s2, errors='coerce'))
print('\n')
Copyright ©2024 Educative, Inc. All rights reserved