Select Page

How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘as_matrix’

by | Programming, Python, Tips

The Pandas method as_matrix is deprecated as of version 0.23.0. If you want to convert a DataFrame to its NumPy array representation, you can use DataFrame.values() or DataFrame.to_numpy.

This tutorial will go through how to solve this error with code examples.


AttributeError: ‘DataFrame’ object has no attribute ‘as_matrix’

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 of the error ‘DataFrame’ object has no attribute ‘as_matrix‘ tells us that the DataFrame object we are handling does not have the as_matrix as an attribute. The as_matrix() method is deprecated as of version 0.23.0, so if you are using a version after 0.23.0 you will get the AttributeError.

Example

Let’s look at an example where we want to convert a DataFrame to a NumPy array. We will start with a CSV file containing pizza names and prices. We will save the file as pizzas.csv.

pizza,price
margherita,£7.99
pepperoni,£8.99
four cheeses,£10.99
funghi,£8.99

Next, we will load the data into a DataFrame using pandas.

import pandas as pd

df = pd.read_csv('pizzas.csv')

print(df)
          pizza   price
0    margherita   £7.99
1     pepperoni   £8.99
2  four cheeses  £10.99
3        funghi   £8.99

We want the pizza prices to be floating-point numbers instead of strings. We will use the string accessor method to remove the £ character and then cast the column to float using astype().

df.price = df.price.str.replace('£','').astype(float)
print(df.price)
0     7.99
1     8.99
2    10.99
3     8.99
Name: price, dtype: float64

Then we will try to convert the DataFrame to a NumPy array using as_matrix.

arr = df.as_matrix()
print(arr)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 arr = df.as_matrix()
      2 print(arr)

File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py:5583, in NDFrame.__getattr__(self, name)
   5576 if (
   5577     name not in self._internal_names_set
   5578     and name not in self._metadata
   5579     and name not in self._accessors
   5580     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5581 ):
   5582     return self[name]
-> 5583 return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'as_matrix'

The error occurs because as_matrix() is a deprecated method.

Solution

To solve this error we can either use DataFrame.values or DataFrame.to_numpy(). Let’s look at how to convert the DataFrame to a NumPy array using values:

arr = df.values

print(arr)

print(type(arr))

Let’s run the code to get the result.

[['margherita' 7.99]
 ['pepperoni' 8.99]
 ['four cheeses' 10.99]
 ['funghi' 8.99]]
<class 'numpy.ndarray'>

Let’s look at how to convert a DataFrame to a NumPy array using to_numpy().

arr = df.to_numpy()

print(arr)

print(type(arr))

Let’s run the code to see the result:

[['margherita' 7.99]
 ['pepperoni' 8.99]
 ['four cheeses' 10.99]
 ['funghi' 8.99]]
<class 'numpy.ndarray'>

Summary

Congratulations on reading to the end of this tutorial! The method as_matrix is deprecated as of pandas version 0.23.0. You can use DataFrame.values or DataFrame.to_numpy instead.

For further reading on deprecated Pandas methods, go to the article:

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!