Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to treat '<attribute 'dtype' of 'numpy.generic' objects>' error?

After installing pypfopt and u-numpy, dataframe.info() command shows this error.

TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
like image 301
A V Avatar asked Mar 10 '21 11:03

A V


People also ask

What is dtype in NumPy?

Data type Object (dtype) in NumPy Python. 1 Type of the data (integer, float, Python object etc.) 2 Size of the data (number of bytes) 3 Byte order of the data (little-endian or big-endian) 4 If the data type is a sub-array, what is its shape and data type.

How to create a data type object in NumPy?

1. Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype. obj: Object to be converted to a data-type object.

How to construct a data type (dtype) object in Python?

Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype. obj: Object to be converted to a data-type object.

How do I check if data is cast to NumPy dtype?

Check input data with np.asarray (data). ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray (data). This error occurs when you attempt to fit a regression model in Python and fail to convert categorical variables to dummy variables first before fitting the model.


5 Answers

I fixed this type error downgrading numpy version to 1.16.5.

Try it!

Use code below in your jupyter notebook to downgrade your numpy:

!pip install numpy==1.16.5

My pandas version: 0.24.2

like image 85
VictorSaraivaRocha Avatar answered Oct 19 '22 10:10

VictorSaraivaRocha


I happened to mix my versions and I encountered the problem today. I managed to fix it. Both codes in jupyter gave me an error: TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type

df.info() 
df.categorical_column_name.value_counts().plot.bar()

I got the error: TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type

This is how i fixed it

Inside jupyter: Check numpy version:

import numpy as np
print(np.__version__)

To upgrade:

!pip3 install numpy --upgrade

Inside Command line check numpy version: python

import numpy
print(numpy.__version__)

if versions are not the same choose whether to upgrade/downgrade: To upgrade:

$pip install numpy --upgrade

To downgrade just specify the version

If you have python environment installed: Go to the right folder: Check the installed version:

$pipenv --version

To verify if you have a pip environment installed for that folder: On your terminal Go to the folder and type:

$pipenv --version

If there is a pipenv it will show the version and if there is none it won't.

check numpy version

$python
>>> import numpy
#prints the version
>>> print(numpy__version__)

To upgrade the version:

>>>exit()

#To install the latest version don't specify the version

$pipenv install numpy

#if you want to downgrade specify the version

$pipenv install numpy=version_type

Do the same for pandas. Note that with pandas if your pandas environment is 1.2.3 on the jupyter notebook upgrade with !pip install pandas==1.2.3 or just !pip install pandas --upgrade --user.

Note that if the commands are giving you an error always include --user at the end of the command.

To create a new environment using miniconda and install updated packages follow the link [https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html][1]

Run the following commands from a terminal window:

  1. conda create -n name_of_my_env python This will create a minimal environment with only Python installed in it. To put your self inside this environment run:

source activate name_of_my_env On Windows the command is: 2. activate name_of_my_env

The final step required is to install pandas. This can be done with the following command:

conda install pandas

To install a specific pandas version:

  1. conda install pandas=0.20.3

I prefer using the latest version of pandas 1.2.3

However the first method should solve your problem. Always restart your notebook by closing and reopening it.

I will stick around to see if you are winning. But this will resolve your problem. The problem is caused by the versions of numpy and pandas [1]: https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html

like image 14
SP_ Avatar answered Oct 19 '22 11:10

SP_


Here's a link to the numpy issue associated with this error: https://github.com/numpy/numpy/issues/18355. A succinct fix is given there (in https://github.com/numpy/numpy/issues/18355#issuecomment-1029684903):

pip install --upgrade numpy
pip install --upgrade pandas
like image 3
mherzog Avatar answered Oct 19 '22 09:10

mherzog


downgrading to numpy==1.19.5 works

Use below command to downgrade in anaconda prompt:

python -m pip install numpy==1.19.5

like image 3
2 revs, 2 users 75% Avatar answered Oct 19 '22 10:10

2 revs, 2 users 75%


The issue is because of the non-compatibility of NumPy and pandas versions. I couldn't downgrade my NumPy for some odd reasons as others suggested from Anaconda. But found this link helpful Downgrading pandas to 1.3 and with the existing NumPy version set at 1.20.1, helped me to overcome this issue.

like image 2
vcnr_1234 Avatar answered Oct 19 '22 09:10

vcnr_1234