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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享
- 2025-01-01告别Anaconda?试试这些替代品吧
- 2024-12-31自学记录鸿蒙API 13:实现人脸比对Core Vision Face Comparator
- 2024-12-31自学记录鸿蒙 API 13:骨骼点检测应用Core Vision Skeleton Detection
- 2024-12-31自学记录鸿蒙 API 13:实现人脸检测 Core Vision Face Detector
- 2024-12-31在C++中的双端队列是什么意思,跟消息队列有关系吗?-icode9专业技术文章分享
- 2024-12-31内存泄漏(Memory Leak)是什么,有哪些原因和优化办法?-icode9专业技术文章分享
- 2024-12-31计算机中的内存分配方式堆和栈有什么关系和特点?-icode9专业技术文章分享
- 2024-12-31QT布局器的具体使用原理和作用是什么?-icode9专业技术文章分享
- 2024-12-30用PydanticAI和Gemini 2.0构建Airflow的AI助手