解决:TypeError: Cannot interpret ‘<attribute ‘dtype‘ of ‘numpy.generic‘ objects>‘ as a data type

傷城~ 2024-05-07 22:08 173阅读 0赞

最近安装了一个新库:

  1. tensorflow

之后我原来的一些代码就不好使了。

比如下面的语句:

  1. df.info()
  2. df['year'].value_counts().plot(kind='bar')

提示错误:

  1. Traceback (most recent call last):
  2. File "D:/pyprogect/excellianxi/条形图的绘制.py", line 6, in <module>
  3. df.info()
  4. File "D:\anaconda2019\lib\site-packages\pandas\core\frame.py", line 2557, in info
  5. mem_usage = self.memory_usage(index=True, deep=deep).sum()
  6. File "D:\anaconda2019\lib\site-packages\pandas\core\frame.py", line 2655, in memory_usage
  7. result = Series(self.index.memory_usage(deep=deep), index=["Index"]).append(
  8. File "D:\anaconda2019\lib\site-packages\pandas\core\series.py", line 311, in __init__
  9. data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  10. File "D:\anaconda2019\lib\site-packages\pandas\core\internals\construction.py", line 712, in sanitize_array
  11. subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
  12. File "D:\anaconda2019\lib\site-packages\pandas\core\dtypes\cast.py", line 1233, in construct_1d_arraylike_from_scalar
  13. subarr = np.empty(length, dtype=dtype)
  14. TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type

问了一下我AI哥:

这段错误信息表示在尝试使用Pandas库绘制条形图时遇到了问题。错误发生在尝试创建一个条形图时,具体的错误类型是TypeError,提示无法将<attribute 'dtype' of 'numpy.generic' objects>解释为一个数据类型。

这个问题通常是由于在df['year']中包含了非数值类型的数据,而条形图需要数值类型的数据来绘制。value_counts()函数用于计算每个唯一值出现的次数,如果df['year']列中有非数值类型的数据,那么在尝试进行数值操作时就会引发这个错误。

要解决这个问题,你可以采取以下步骤:

  1. 检查df['year']列的数据类型,确保它只包含数值类型的数据。你可以使用df['year'].dtypes来查看数据类型。
  2. 如果df['year']列中确实包含了非数值类型的数据,你需要进行数据清洗,将这些非数值类型的数据转换为数值类型或者从数据集中移除。
  3. 一旦确认df['year']列只包含数值类型的数据,再次尝试绘制条形图。

但是我发现的我的数据没啥问题啊,于是我就想是不上两天安装库影响我的pandas了,于是开始:更新库。

  1. pip install --upgrade pandas

很好!解决问题了!

发表评论

表情:
评论列表 (有 0 条评论,173人围观)

还没有评论,来说两句吧...

相关阅读