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.
Table of contents
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.
Example
Consider the following CSV file, pizzas.csv
:
pizza,price margherita,£7.99 pepperoni,£8.99 four cheeses,£10.99 funghi,£8.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') df
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 data
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) new_data
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) new_df.to_csv('new_pizzas.csv')
--------------------------------------------------------------------------- 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:
type(new_data)
numpy.ndarray
Solution
We can convert the array to a DataFrame without using values. Let’s look at the revised code:
new_df = pd.DataFrame(new_data) new_df.to_csv('new_pizzas.csv')
The above code will run with no error and will write a new CSV file called new_pizzas.csv
in the working directory.
Summary
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.