dataframe’ object has no attribute ‘to_numpy’ ( Solved )

dataframe' object has no attribute 'to_numpy' ( Solved )

If you want to manipulate your datasets then pandas allow you to do so. You have to first convert the datasets to dataframe to perform any tasks on the datasets. Suppose you want to convert the dataframe to numpy and you are getting dataframe’ object has no attribute ‘to_numpy’ error then how you can solve it? In this post, you will learn how to solve this error.

What is Numpy?

Numpy is a Python package that allows you to create an array and perform mathematical calculations on it. There are many inbuilt functions that allow you to perform computations in an efficient way.

What causes AttributeError: dataframe’ object has no attribute ‘to_numpy’ Error?

The root cause for getting this error is that you must be using an old version of the Panda’s package. Use the below lines of code to check the version of the panda’s package.

import pandas as pd
print(pd.__version__)

If the version of the pandas is less than 0.24 then you are getting this error as that version of pandas doesn’t support the to_numpy() function.

You will get the error when you will run the below line of code.

import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame({'col1': list('pqrstuv'), 'col2': np.random.choice(10, 7)})
df["col1"].to_numpy()

Output

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

Solution of dataframe’ object has no attribute ‘to_numpy’ error

The solution to this attributeError is very simple. If you want to use the old version of the panda’s package then use the dataframe.values to convert the dataframe to a numpy array.

The second solution is to upgrade the pandas version to use the to_numpy() method. Use the following command to update the pandas module.

conda

conda update pandas

To upgrade pandas under Python3

pip3 install --upgrade pandas

Now you will not get the error when you will run the below lines of code.

import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame({'col1': list('pqrstuv'), 'col2': np.random.choice(10, 7)})
df["col1"].to_numpy()

Output

using to_numpy function to convert dataframe to numpy array
using to_numpy function to convert dataframe to numpy array

Conclusion

If you want to convert the dataframe columns to a numpy array then you can use the .values if the version of the pandas is less than 0.24. The upper version supports the to_numpy() function to convert the dataframe to numpy. If you are getting the to_numpy attributeError then the above solution will solve it.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner