14-9 的输出差异问题,请教老师 (python 版本 3.11.7)
来源:14-9 【数据统计】检索日期列并列出所有2020年的感染数据

慕虎0285045
2024-06-30
14-9 的输出问题,请教老师 (python 版本 3.11.7)
=代码===============================================
china_2020_data_2 = china_data[ china_data[‘Date_reported’].dt.year == 2020 ] # 1 suggests with loc
china_2020_data_2
原本没有出现问题(错误信息),出现了如下的AttributeError
AttributeError Traceback (most recent call last)
Cell In[20], line 1
----> 1 china_2020_data_2 = china_data[ china_data[‘Date_reported’].dt.year == 2020 ] # 1 suggests with loc
3 china_2020_data_2
File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/generic.py:5902, in NDFrame.getattr(self, name)
5895 if (
5896 name not in self._internal_names_set
5897 and name not in self._metadata
5898 and name not in self._accessors
5899 and self._info_axis._can_hold_identifiers_and_holds_name(name)
5900 ):
5901 return self[name]
-> 5902 return object.getattribute(self, name)
File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/accessor.py:182, in CachedAccessor.get(self, obj, cls)
179 if obj is None:
180 # we’re accessing the attribute of the class, i.e., Dataset.geo
181 return self._accessor
–> 182 accessor_obj = self._accessor(obj)
183 # Replace the property with the accessor object. Inspired by:
184 # https://www.pydanny.com/cached-property.html
185 # We need to use object.setattr because we overwrite setattr on
186 # NDFrame
187 object.setattr(obj, self._name, accessor_obj)
File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/indexes/accessors.py:512, in CombinedDatetimelikeProperties.new(cls, data)
509 elif is_period_dtype(data.dtype):
510 return PeriodProperties(data, orig)
–> 512 raise AttributeError(“Can only use .dt accessor with datetimelike values”)
AttributeError: Can only use .dt accessor with datetimelike values
而进一步优化的loc的最后一段范例code,则仍旧出现”警告信息“,即使加入了loc函数的应用外”china_data.loc[:,‘Date_reported’])“,但预期输出的图表还是输出了
=代码==================================
china_data.loc[:, ‘Date_reported_datetime_2’] = pd.to_datetime(china_data.loc[:,‘Date_reported’]) # with loc and see how the code can be improved
china_2020_data_3 = china_data[ china_data[‘Date_reported_datetime_2’].dt.year == 2020 ]
china_2020_data_3
=错误信息======================================
/var/folders/dz/y_f0124n7pb8kxctwh0pb2vw0000gn/T/ipykernel_26259/1623847232.py:2: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
china_data.loc[:, ‘Date_reported_datetime_2’] = pd.to_datetime(china_data.loc[:,‘Date_reported’]) # with loc and see how the code can be improved
/var/folders/dz/y_f0124n7pb8kxctwh0pb2vw0000gn/T/ipykernel_26259/1623847232.py:2: DeprecationWarning: In a future version, df.iloc[:, i] = newvals
will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either df[df.columns[i]] = newvals
or, if columns are non-unique, df.isetitem(i, newvals)
china_data.loc[:, ‘Date_reported_datetime_2’] = pd.to_datetime(china_data.loc[:,‘Date_reported’]) # with loc and see how the code can be improved
Date_reported Country_code Country WHO_region New_cases Cumulative_cases New_deaths Cumulative_deaths Date_reported_datetime Date_reported_datetime_2
52584 2020-01-03 CN China WPRO 0 0 0 0 2020-01-03 2020-01-03
52585 2020-01-04 CN China WPRO 1 1 0 0 2020-01-04 2020-01-04
52586 2020-01-05 CN China WPRO 0 1 0 0 2020-01-05 2020-01-05
52587 2020-01-06 CN China WPRO 3 4 0 0 2020-01-06 2020-01-06
52588 2020-01-07 CN China WPRO 0 4 0 0 2020-01-07 2020-01-07
… … … … … … … … … … …
52943 2020-12-27 CN China WPRO 84 96324 0 4777 2020-12-27 2020-12-27
52944 2020-12-28 CN China WPRO 93 96417 1 4778 2020-12-28 2020-12-28
52945 2020-12-29 CN China WPRO 96 96513 4 4782 2020-12-29 2020-12-29
52946 2020-12-30 CN China WPRO 79 96592 2 4784 2020-12-30 2020-12-30
52947 2020-12-31 CN China WPRO 81 96673 4 4788 2020-12-31 2020-1
1回答
-
小布_老师
2024-06-30
文字信息不好找,图片会更好看错误。
在问题中找到
china_2020_data_2 = china_data[ china_data[‘Date_reported’].dt.year == 2020 ]
AttributeError: Can only use .dt accessor with datetimelike values
报错信息是指属性错误,AttributeError: 只能对日期时间类型的值使用 .dt 访问器
应该是代码的运行,某个代码参数的类型错了
你把代码细分,一行代码尽可能的拆开写,
看报错的,具体是哪个参数
00
相似问题