首页 > 解决方案 > 在 Linux 中 DatetimeArray 到 dtype datetime64 但在 Windows 中很好

问题描述

我有一个使用 Pandas 读取 Excel 文件的 Python3 程序。该程序在 Windows 10 (Python3.7) 中运行良好,但在 Ubuntu 服务器 (Python3.5) 中抛出错误。跟踪表明这是一个熊猫问题。有没有人有同样的经历?如何解决?

    df = pandas.read_excel(source, sheet_name='Sheet1', converters = { 'Date Time': pandas.to_datetime})
    df['Date Time'] = df['Date Time'].dt.tz_localize('US/Mountain').dt.tz_convert('UTC')
    df['Net Load Forecast'] = df['Net Load Forecast'].fillna(0.0).astype(int)
    df['target_hour'].astype('datetime64') #<<---this line throws error in Ubuntu

错误

 File "data_ftp_to_etrm_import.py", line 72, in <module>
    df['target_hour'] = df['target_hour'].astype('datetime64')
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/generic.py", line 5681, in astype
    **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/internals/managers.py", line 531, in astype
    return self.apply('astype', dtype=dtype, **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/internals/managers.py", line 395, in apply
    applied = getattr(b, f)(**kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/internals/blocks.py", line 534, in astype
    **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/internals/blocks.py", line 2139, in _astype
    return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/internals/blocks.py", line 616, in _astype
    values = self.values.astype(dtype)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/arrays/datetimes.py", line 642, in astype
    return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/pandas/core/arrays/datetimelike.py", line 540, in astype
    raise TypeError(msg.format(name=type(self).__name__, dtype=dtype))

标签: python-3.xpandas

解决方案


推荐阅读