如何处理 'numpy.generic' 对象的 '<属性 'dtype' 错误?

A V*_*A V 4 typeerror dataframe python-3.x pandas

安装 pypfopt 和 u-numpy 后,dataframe.info()命令显示此错误。

TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
Run Code Online (Sandbox Code Playgroud)

小智 13

我修复了此类型错误,将numpy版本降级为1.16.5.

尝试一下!

在您的笔记本中使用以下代码jupyter来降级您的numpy:

!pip install numpy==1.16.5

我的pandas版本:0.24.2


SP_*_*SP_ 7

我碰巧混合了我的版本,今天我遇到了这个问题。我设法解决了它。jupyter 中的两个代码都给了我一个错误:TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type

df.info() 
df.categorical_column_name.value_counts().plot.bar()
Run Code Online (Sandbox Code Playgroud)

我收到错误:TypeError:无法将 'numpy.generic' objects>' 的 '<attribute 'dtype' 解释为数据类型

这就是我修复它的方式

jyputer 内部:检查 numpy 版本:

import numpy as np
print(np.__version__)
Run Code Online (Sandbox Code Playgroud)

升级:

!pip3 install numpy --upgrade
Run Code Online (Sandbox Code Playgroud)

内部命令行检查 numpy 版本:python

import numpy
print(numpy.__version__)
Run Code Online (Sandbox Code Playgroud)

如果版本不一样,选择是否升级/降级: 升级:

$pip install numpy --upgrade
Run Code Online (Sandbox Code Playgroud)

降级只需指定版本

如果安装了python环境:转到正确的文件夹:检查安装的版本:

$pipenv --version
Run Code Online (Sandbox Code Playgroud)

要验证您是否为该文件夹安装了 pip 环境: 在您的终端上转到该文件夹​​并键入:

$pipenv --version
Run Code Online (Sandbox Code Playgroud)

如果有pipenv,它将显示版本,如果没有,则不会。

检查 numpy 版本

$python
>>> import numpy
#prints the version
>>> print(numpy__version__)
Run Code Online (Sandbox Code Playgroud)

要升级版本:

>>>exit()
Run Code Online (Sandbox Code Playgroud)

#安装最新版本不要指定版本

$pipenv install numpy
Run Code Online (Sandbox Code Playgroud)

#如果要降级指定版本

$pipenv install numpy=version_type
Run Code Online (Sandbox Code Playgroud)

对熊猫做同样的事情。请注意,如果您在 jupyter notebook 上的 pandas 环境为 1.2.3,请使用 pandas 升级 !pip install pandas==1.2.3 或 !pip install pandas --upgrade --user

请注意,如果命令给您一个错误,请始终在命令末尾包含 --user。

要使用 miniconda 创建新环境并安装更新的软件包,请点击链接 [https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html][1]

从终端窗口运行以下命令:

  1. conda create -n name_of_my_env python 这将创建一个仅安装 Python 的最小环境。把你自己放在这个环境中运行:

source activate name_of_my_env 在 Windows 上命令是: 2. activate name_of_my_env

所需的最后一步是安装熊猫。这可以通过以下命令完成:

conda install pandas 要安装特定的 Pandas 版本:

  1. 畅达安装熊猫=0.20.3

我更喜欢使用最新版本的pandas 1.2.3

但是,第一种方法应该可以解决您的问题。始终重新启动您的笔记本,我关闭它并重新打开它。

我会留下来看看你是否赢了。但这将解决您的问题。该问题是由 numpy 和 pandas [1] 的版本引起的:https : //pandas.pydata.org/pandas-docs/stable/getting_started/install.html


小智 6

降级到 numpy==1.19.5 有效

使用以下命令在 anaconda 提示符下降级:

python -m pip install numpy==1.19.5