python - numpy 分配给具有浮点数的日期的 dtype 数组导致“无法将字符串转换为浮点数:'2017-01-01T01:01:01'”错误
问题描述
为什么以下 numpy 数组赋值会导致 ValueError: 'could not convert string to float: '2017-01-01T01:01:01''
import numpy as np
r=['2017-01-01T01:01:01','61.380001068115234']
t = np.dtype([('d', 'datetime64[s]'), ('o', 'f4')])
s = np.array(r, dtype=t)
python 3.8.2,Windows 10,numpy 1.19.1。
解决方案
您将数据类型定义为 datetime 和 float 的元组,因此数组中的每个项目都应该是这样的元组。
这是正确的:
np.array([('2017-01-01T01:01:01', '61.380001068115234')], dtype=t)
如果数组中有多个项目,它将如下所示:
np.array([
('2017-01-01T01:01:01', '61.380001068115234'),
('2017-01-02T01:01:01', '62.380001068115234'),
('2017-01-03T01:01:01', '63.380001068115234'),
], dtype=t)
推荐阅读
- c# - GhostScript.Net 加速
- networking - 移动互联网中的本地IP和外部IP
- kubernetes - Kubernetes 中的 CockroachDB 快照备份
- javascript - 如何在 html 中包含巨大的 javascript js 文件?
- python-3.x - 尝试将每个第 x 个字符转换为大写
- linux - 如何为 Qt 应用程序制作嵌入式树莓派操作系统
- python - 为 Mask-RCNN 加载掩码(VGG 图像注释器)时出现问题
- java - 如何包装注释并有条件地将其应用于方法
- go - return &obj 是什么意思?它对平等检查有什么影响
- amazon-ec2 - 删除公共 ElasticIP