• Post author:
  • Post category:Pandas
  • Post last modified:March 28, 2024
  • Reading time:17 mins read
You are currently viewing Use pandas.to_numeric() Function

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.

Key Points –

  • Pandas.to_numeric() function converts argument to a numeric type, handling errors or non-numeric values gracefully.
  • It offers flexibility in handling different data types, allowing for customization in handling errors, coercion, and downcasting.
  • Pandas.to_numeric() facilitates conversion of non-numeric data types (such as strings or objects) to numeric types, essential for numerical analysis and computations.
  • Pandas.to_numeric() function is particularly useful for data cleaning and preparation tasks in data analysis and manipulation workflows, aiding in the transformation of heterogeneous data into a consistent numeric format.
  • It automatically handles errors and non-numeric values, providing options to customize how such cases are treated, such as raising errors, ignoring errors, or converting errors to NaN values.

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.


#  Quick examples of pandas.to_numeric function

# 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

Complete Example


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)

FAQ’s of pandas.to_numeric() Function

What is the purpose of pandas.to_numeric() function?

The purpose of pandas.to_numeric() function is to convert elements of an object to numeric types. This function is particularly useful when dealing with datasets containing mixed data types and needing numeric analysis.

How does pandas.to_numeric() handle non-numeric values?

By default, pandas.to_numeric() function raises an error if it encounters non-convertible elements. However, you can specify the errors parameter to control this behavior. Setting errors='coerce' will convert non-numeric values to NaN (Not a Number) without raising an error.

What happens if errors=’ignore’ is used with pandas.to_numeric()?

When errors='ignore' is used, pandas.to_numeric() function will ignore non-convertible elements and leave them unchanged in the resulting Series. No error will be raised in this case.

Is it possible to downcast the data types using pandas.to_numeric()?

pandas.to_numeric() can be applied to DataFrame columns. You can use it directly on a DataFrame column or within DataFrame operations to convert specific columns to numeric types.

What is the default behavior of pandas.to_numeric() for handling errors?

By default, pandas.to_numeric() function raises an error (errors='raise') when encountering non-convertible elements. This behavior ensures data integrity by alerting the user to potential issues.

Can pandas.to_numeric() handle string representations of negative numbers?

pandas.to_numeric() can handle string representations of negative numbers. It correctly converts them to their corresponding numeric values, including negative signs.

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

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.