pandas 转化DataFrame后输出报错 AttributeError: 'NoneType' object has no attribute 'total_sec
2022/9/14 23:20:45
本文主要是介绍pandas 转化DataFrame后输出报错 AttributeError: 'NoneType' object has no attribute 'total_sec,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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')
这篇关于pandas 转化DataFrame后输出报错 AttributeError: 'NoneType' object has no attribute 'total_sec的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享
- 2024-11-22ansible 的archive 参数是什么意思?-icode9专业技术文章分享
- 2024-11-22ansible 中怎么只用archive 排除某个目录?-icode9专业技术文章分享
- 2024-11-22exclude_path参数是什么作用?-icode9专业技术文章分享
- 2024-11-22微信开放平台第三方平台什么时候调用数据预拉取和数据周期性更新接口?-icode9专业技术文章分享
- 2024-11-22uniapp 实现聊天消息会话的列表功能怎么实现?-icode9专业技术文章分享
- 2024-11-22在Mac系统上将图片中的文字提取出来有哪些方法?-icode9专业技术文章分享
- 2024-11-22excel 表格中怎么固定一行显示不滚动?-icode9专业技术文章分享
- 2024-11-22怎么将 -rwxr-xr-x 修改为 drwxr-xr-x?-icode9专业技术文章分享
- 2024-11-22在Excel中怎么将小数向上取整到最接近的整数?-icode9专业技术文章分享