首页 > 解决方案 > 不一致的 AttributeError:“str”对象没有属性

问题描述

我正在学习如何使用 Pandas、Seaborn 和 Numpy 从 CSV 数据集创建热图。

# Canada Cases Year overview - Heatmap

# Read file and separate needed data subset
canada_df = pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/main/data/countries-aggregated.csv', usecols = [0, 1, 2], index_col = 0, parse_dates=[0])
canada_df.info()
canada_df.head()

<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 110370 个条目,2020-01-22 到 2021-08-09 数据列(共 2 列):

# 柱子 非空 数数 类型
0 国家 110370 非空 目的
1 确认的 110370 非空 整数64

数据类型:int64(1),对象(1)

国家 确认的
日期 阿富汗 0
2020-01-22 阿富汗 0
2020-01-23 阿富汗 0
2020-01-24 阿富汗 0
2020-01-25 阿富汗 0
2020-01-26 阿富汗 0
#Filtering data for Canadian values only
canada_df.loc[canada_df['Country']=='Canada']

#Isolating needed subset
canada_cases = canada_df['Confirmed']
canada_cases.head()

# create a copy of the dataframe, and add columns for month and year
canada_heatmap = canada_cases.copy()
canada_heatmap['month'] = [i.month for i in canada_heatmap.index]
canada_heatmap['year'] = [i.year for i in canada_heatmap.index]

# group by month and year, get the average
canada_heatmap = canada_heatmap.groupby(['month', 'year']).mean()

此时我收到此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-787f01af1859> in <module>
      2 canada_heatmap = canada_cases.copy()
      3 canada_heatmap['month'] = [i.month for i in canada_heatmap.index]
----> 4 canada_heatmap['year'] = [i.year for i in canada_heatmap.index]
      5 # group by month and year, get the average
      6 canada_heatmap = canada_heatmap.groupby(['month', 'year']).mean()

<ipython-input-54-787f01af1859> in <listcomp>(.0)
      2 canada_heatmap = canada_cases.copy()
      3 canada_heatmap['month'] = [i.month for i in canada_heatmap.index]
----> 4 canada_heatmap['year'] = [i.year for i in canada_heatmap.index]
      5 # group by month and year, get the average
      6 canada_heatmap = canada_heatmap.groupby(['month', 'year']).mean()

AttributeError: 'str' object has no attribute 'year'

我被困在如何解决这个问题上,因为上面的行几乎相同,但没有引发同样的问题。有谁知道这里发生了什么?

标签: pythonpandasdataframenumpy

解决方案


您的某些索引不是日期格式(2 个元素是字符串,它们是两个 last 元素)

# check the type of the elements in index
count = pd.Series(canada_heatmap.index).apply(type).value_counts()
print(count)

<class 'pandas._libs.tslibs.timestamps.Timestamp'>    110370
<class 'str'>                                              2
Name: Date, dtype: int64

# remove them
canada_heatmap = canada_heatmap.iloc[:-2]

推荐阅读