解决:TypeError: Cannot interpret ‘<attribute ‘dtype‘ of ‘numpy.generic‘ objects>‘ as a data type
最近安装了一个新库:
tensorflow
之后我原来的一些代码就不好使了。
比如下面的语句:
df.info()
df['year'].value_counts().plot(kind='bar')
提示错误:
Traceback (most recent call last):
File "D:/pyprogect/excellianxi/条形图的绘制.py", line 6, in <module>
df.info()
File "D:\anaconda2019\lib\site-packages\pandas\core\frame.py", line 2557, in info
mem_usage = self.memory_usage(index=True, deep=deep).sum()
File "D:\anaconda2019\lib\site-packages\pandas\core\frame.py", line 2655, in memory_usage
result = Series(self.index.memory_usage(deep=deep), index=["Index"]).append(
File "D:\anaconda2019\lib\site-packages\pandas\core\series.py", line 311, in __init__
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
File "D:\anaconda2019\lib\site-packages\pandas\core\internals\construction.py", line 712, in sanitize_array
subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
File "D:\anaconda2019\lib\site-packages\pandas\core\dtypes\cast.py", line 1233, in construct_1d_arraylike_from_scalar
subarr = np.empty(length, dtype=dtype)
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']
列中有非数值类型的数据,那么在尝试进行数值操作时就会引发这个错误。
要解决这个问题,你可以采取以下步骤:
- 检查
df['year']
列的数据类型,确保它只包含数值类型的数据。你可以使用df['year'].dtypes
来查看数据类型。 - 如果
df['year']
列中确实包含了非数值类型的数据,你需要进行数据清洗,将这些非数值类型的数据转换为数值类型或者从数据集中移除。 - 一旦确认
df['year']
列只包含数值类型的数据,再次尝试绘制条形图。
但是我发现的我的数据没啥问题啊,于是我就想是不上两天安装库影响我的pandas了,于是开始:更新库。
pip install --upgrade pandas
很好!解决问题了!
还没有评论,来说两句吧...