python-3.x - 在 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))
解决方案
推荐阅读
- xamarin.forms - Xamaria.form InitializeComponent 在此上下文中不存在
- pharo - 如何将元素动态添加到 Roassal RTGrapher 实例?
- php - 这是保护用户帐户的可行方法吗?
- ubuntu - Curl:即使应用程序正在运行,连接也被拒绝
- javascript - 元素无法溢出html css
- python - 返回日期格式的 Python 库
- django - 使用 Django 的 pre_delete 信号创建新对象
- javascript - 为什么我对 selection.join() 的调用没有返回输入选择?
- android - 如何更改操作栏上的返回图标和标签
- windows - 为不存在的目录创建符号链接 - Windows