Select Page

How to Solve Python AttributeError: ‘numpy.ndarray’ has no attribute ‘values’

by | Programming, Python, Tips

If you attempt to call the values() method on a NumPy array, you will raise the error AttributeError: ‘numpy.ndarray’ object has no attribute ‘values’. The values() method belongs to the DataFrame object. This error typically occurs when trying to call values after already converting a Series or DataFrame to NumPy array. You can avoid this error by checking the type of the object before calling values.

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

AttributeError: ‘numpy.ndarray’ object has no attribute ‘values’

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 values method is a DataFrame method, not a numpy.ndarray method which returns a NumPy representation of a DataFrame.


Consider the following CSV file, pizzas.csv:

four cheeses,£10.99

We will attempt to load this file into a DataFrame using pandas.read_csv to append a new pizza and its price.

import pandas as pd
import numpy as np

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

 pizza   price
0    margherita   £7.99
1     pepperoni   £8.99
2  four cheeses  £10.99
3        funghi   £8.99

Now that we have the DataFrame, we can convert it to a NumPy array using values:

data = df.values
array([['margherita', '£7.99'],
       ['pepperoni', '£8.99'],
       ['four cheeses', '£10.99'],
       ['funghi', '£8.99']], dtype=object)

We can append a new pizza to the list using numpy.append:

new_data = np.append(data, np.array([['tartufo', '£14.99']]), axis=0)

Let’s try to get the pizza data back into a DataFrame and save it to a new CSV file.

new_df = pd.DataFrame(new_data.values)
AttributeError                            Traceback (most recent call last)
Input In [48], in <cell line: 1>()
----> 1 new_df = pd.DataFrame(new_data.values)

AttributeError: 'numpy.ndarray' object has no attribute 'values'

The error occurs because the new_data variable is a numpy.ndarray, which does not have values as an attribute. We can verify that new_data is a numpy.ndarray using the built-in type() method:



We can convert the array to a DataFrame without using values. Let’s look at the revised code:

new_df = pd.DataFrame(new_data)

The above code will run with no error and will write a new CSV file called new_pizzas.csv in the working directory.


Congratulations on reading to the end of this tutorial! If you encounter this error, ensure that you check the type of object that is throwing the error using the type() method. If the object is a NumPy ndarray you can remove the values method call.

For further reading on AttributeErrors, go to the article:

How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘drop’

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!