python - 不一致的 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'
我被困在如何解决这个问题上,因为上面的行几乎相同,但没有引发同样的问题。有谁知道这里发生了什么?
解决方案
您的某些索引不是日期格式(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]
推荐阅读
- hadoop - Kubernetes 分布式文件系统
- unit-testing - 替代现在废弃的 dbunit 扩展?
- amazon-web-services - 使用 S3 进行上传但不允许公共访问
- ionic-framework - 是否可以在没有受信任证书的情况下共享 .appx?如果不是,我如何在不发布的情况下共享/分发我的应用程序?
- c# - Trim().Split 在 Contains() 中导致问题
- datagridview - 如何将程序创建的数据表显示到动态创建的 datagridView?
- drools - 通过检查levenshtein距离在流口水中进行类似的匹配而不是精确匹配
- sql - Oracle SQL - 显示当前记录的值
- r - ggplot2:如何在 R 中正确格式化 sec.axis
- android - 添加从 Unity for Android studio 导出的 2 个模块出现错误:“错误:程序类型已存在:bitter.jnibridge.JNIBridge”