Use pandas.to_numeric() Function

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:December 26, 2022

Pandas.to_numeric() function is used to convert the passed argument to a numeric type. The default return type of the function is float64 or int64 depending on the input. You can use the downcast parameter if you want to convert data to a particular type.

In this article, I will explain how to use pandas.to_numeric() function by using its syntax, and parameters and explaining how to convert from string to a numeric type in Pandas.

1. Quick Examples of pandas.to_numeric Function

If you are in a hurry, below are some quick examples of how to use pandas.to_numeric() function.


# Below are quick examples

# Example 1: Use pandas.to_numeric() function
ser2 = pd.to_numeric(ser)

# Example 2: Convert a series to numeric using to_numeric()
ser2 = pd.to_numeric(ser, downcast='float')

# Example 3: Convert a Series to integer using to_numeric()
ser2 = pd.to_numeric(ser, downcast='signed')

# Example 4: Using errors=’ignore’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='ignore')

# Example 5: Using errors=’coerce’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='coerce')

2. Syntax of pandas.to_numeric()

Following is the syntax of pandas.to_numeric() function.


# Syntax of pandas.to_numeric()
pandas.to_numeric(arg, errors='raise', downcast=None)

2.1 Parameters of to_numeric()

Following are the parameters of pandas to_numeric() function

  • arg – It is a scalar, list, tuple, 1-d array, or Series. It is an argument you want to be converted.
  • errors – It is a string parameter. It has three options: {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’.
    • If it is ‘raise’, then invalid parsing will set an exception
    • If ‘coerce’, then invalid parsing will be set as NaN.
    • If ‘ignore’, then invalid parsing will return the input.
  • downcast – It is a string parameter. It has four options: integer 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.
    • ‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8)
    • ‘unsigned’: smallest unsigned int dtype (min.: np.uint8)
    • ‘float’: smallest float dtype (min.: np.float32)

2.2 Return value of to_numeric()

Pandas.to_numeric() function returns a numeric type data. Also, returns the pandas series if the input is Series; otherwise, it will return ndarray.

Initialize Pandas Series

Now, let’s create pandas series using a list of values. Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.


import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series(['2.0','4','6.0',-7])
print(ser)

Yields below output.


# Output
0    2.0
1      4
2    6.0
3     -7
dtype: object

3. Use pandas.to_numeric() Function

Pandas.to_numeric() function is used to convert the given argument to a numeric type, for example, convert the values of series into numeric.


# Use pandas.to_numeric() function
ser2 = pd.to_numeric(ser)
print(ser2)

# Output
0    2.0
1      4
2    6.0
3     -7
dtype: object

5. Convert a Series to Numeric using pandas.to_numeric()

You can pass downcast='float' into pandas.to_numeric() function to convert a series to float. For example use pd.to_numeric(ser,downcast='float') to return the numeric series. To change it to a particular data type, You need to pass the downcast parameter with reasonable arguments.


# Convert a series to numeric using to_numeric()
ser2 = pd.to_numeric(ser, downcast='float')
print(ser2)

# Output
0    2.0
1    4.0
2    6.0
3   -7.0
dtype: float32

6. Convert a Series to an integer using pandas.to_numeric()

Alternatively, you can pass downcast='signed' to pandas.to_numeric() function to use the smallest possible integer that can hold the values. For example use pd.to_numeric(ser, downcast='signed') to return the int8 type series.


# Convert a Series to integer using to_numeric()
ser2 = pd.to_numeric(ser, downcast='signed')
print(ser2)

# Output
0    2
1    4
2    6
3   -7
dtype: int8

7. Using errors=’ignore’

When we pass the given Series which contains string objects along with numerical values, into pandas.to_numeric() function, it will return the Value Error because this function can’t be parsed string objects into numerical type.

Alternatively, set errors param as ignore to not throw an error. Let’s see the below example,


# Using errors=’ignore’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='ignore')
print(ser2)

Yields below output.


# Output
0    Marks
1       22
2     38.5
3       45
4      -32
dtype: object

8. Using errors=’coerce’

You can pass errors=’coerce’ to pandas.to_numeric() function. It will replace all non-numeric values with NaN.


# Using errors=’coerce’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='coerce')
print(ser2)

Yields below output.


# Output
0     NaN
1    22.0
2    38.5
3    45.0
4   -32.0
dtype: float64

9. Complete Example pandas.to_numeric Function


import pandas as pd
import numpy as np
# Initialize pandas series
ser = pd.Series(['2.0','4','6.0',-7])
print(ser)

# Use pandas.to_numeric() function
ser2 = pd.to_numeric(ser)
print(ser2)

# Convert a series to numeric using to_numeric()
ser2 = pd.to_numeric(ser, downcast='float')
print(ser2)

# Convert a Series to integer using to_numeric()
ser2 = pd.to_numeric(ser, downcast='signed')
print(ser2)

# Using errors=’ignore’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='ignore')
print(ser2)

# Using errors=’coerce’
ser = pd.Series(['Marks', 22, 38.5, 45, -32])
ser2 = pd.to_numeric(ser, errors ='coerce')
print(ser2)

10. Conclusion

In this article, I have explained how to use pandas.to_numeric() function. It is used to convert the given argument to a numeric type with examples.

Happy Learning !!

Related Articles

Leave a Reply