In Python, a Pandas Series is a one-dimensional labelled array capable of holding data of any type. Pandas Series is the same as a column in an Excel spreadsheet. If you have a Series containing string values that you want to convert to numeric values, you cannot call to_numeric() on the Series. If you try to call to_numeric on a Series, you will raise the AttributeError: ‘Series’ object has no attribute ‘to_numeric’. to_numeric is a built-in Pandas method, which can accept a Series object as an argument, for example, pandas.to_numeric(series).

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘Series’ object has no attribute ‘to_numeric’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘Series’ object has no attribute ‘to_numeric’” tells us that the Series object we are handling does not have the to_numeric attribute. The to_numeric() method is a built-in Pandas method that we can use to convert a Series argument to a numeric type. We cannot call to_numeric on a Series like series.to_numeric(). Instead, we have to pass the Series to the to_numeric() method. The syntax for the to_numeric() is as follows:

pandas.to_numeric(arg, errors, downcast)

Parameters

  • arg: Required. The argument to convert. It can be a scalar, list, tuple, 1D-array or Series.
  • errors {'ignore', 'raise', 'coerce'}: Optional. How to deal with values that cannot be parsed as a numeric
    • 'raise': raise an error
    • 'coerce': Convert to a NaN
    • 'ignore': Leave value as is.
  • downcast: Optional. Whether or not to convert numerics to the smallest numeric type (e.g. int64 to int8):
    • 'integer' Convert type to np.int8
    • 'signed' Convert type to np.int8
    • 'unsigned' Convert type to np.uint8
    • 'float' Convert type to np.float32
    • None Do not perform any downcasting

Note that the method performs downcasting after the main numeric parsing. If there are parsing issues during downcasting, the method will still raise an error regardless of the errors setting.

Returns

If arg is a Series, then return a new Series. Otherwise, return a new Numpy array.

Example

Let’s look at an example of defining a Series containing numerical string values. We want to convert the values to floating-point numbers using the to_numeric() method. Let’s look at the code:

import pandas as pd

s = pd.Series(["1.", "2.0", "3.4", "5.6"])

s = s.to_numeric()

print(s)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-e5a10f0953de> in <module>
      3 s = pd.Series(["1.", "2.0", "3.4", "5.6"])
      4 
----> 5 s = s.to_numeric()
      6 
      7 print(s)

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5581         ):
   5582             return self[name]
-> 5583         return object.__getattribute__(self, name)
   5584 
   5585     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'to_numeric'

The Python interpreter raises the AttributeError because to_numeric() method is not a Series method.

Solution

To solve this error, we need to pass the Series object to the built-in to_numeric() method using pd.Series(...). Let’s look at the revised code:

import pandas as pd

s = pd.Series(["1.", "2.0", "3.4", "5.6"])

s = pd.to_numeric(s)

print(s)

Let’s run the code to see the result:

0    1.0
1    2.0
2    3.4
3    5.6
dtype: float64

We successfully have a Series object where the values are all of type float64.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘Series’ object has no attribute ‘to_numeric’ occurs when you try to call the to_numeric() method on a Series object. to_numeric() is a built-in method, which means you can call it directly once you have imported Pandas. For example:

import pandas as pd

pd.to_numeric(...)

For further reading on Series, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!