pandas 转化DataFrame后输出报错 AttributeError: 'NoneType' object has no attribute 'total_seconds'
pandas 转化 数据为DataFrame后,DataFrame不能够print
否则会报错AttributeError: 'NoneType' object has no attribute 'total_seconds'
#data的数据结构大致为[{...,'datetime':datetime.datetime(2022, 7, 4, 13, 55, 0, 500000, tzinfo=zoneinfo.ZoneInfo(key='Asia/Shanghai')),...},{}]
#其中包含的datetime是出现错误的原因
df = pd.DataFrame(data)
df['datetime']
Traceback (most recent call last):
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\core\series.py", line 1504, in __repr__
return self.to_string(**repr_params)
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\core\series.py", line 1565, in to_string
result = formatter.to_string()
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\io\formats\format.py", line 391, in to_string
fmt_values = self._get_formatted_values()
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\io\formats\format.py", line 375, in _get_formatted_values
return format_array(
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\io\formats\format.py", line 1316, in format_array
return fmt_obj.get_result()
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\io\formats\format.py", line 1347, in get_result
fmt_values = self._format_strings()
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\io\formats\format.py", line 1810, in _format_strings
values = self.values.astype(object)
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\core\arrays\datetimes.py", line 666, in astype
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)
File "D:\RuanJianAnZhuang\miniconda\envs\myvn\lib\site-packages\pandas\core\arrays\datetimelike.py", line 415, in astype
converted = ints_to_pydatetime(
File "pandas\_libs\tslibs\vectorized.pyx", line 158, in pandas._libs.tslibs.vectorized.ints_to_pydatetime
File "pandas\_libs\tslibs\timezones.pyx", line 266, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
解决办法:指定时区
df['datetime'] = df['datetime'].dt.tz_convert('Asia/Shanghai')
出处: https://www.cnblogs.com/meizhengchao/p/16694569.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(meizhengchao@qq.com)咨询.