C/C++教程

pandas 转化DataFrame后输出报错 AttributeError: 'NoneType' object has no attribute 'total_sec

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!