我想知道。如何将日期时间列添加到 np.array。我写了这段代码。但它不起作用,我得到一个异常“无法从 NumPy 日期时间或时间增量中的特定单位转换为通用单位”
trade_type = np.dtype(
[
('id', 'U'),
('timestamp', 'u8'),
('datetime', np.dtype('datetime64'))
]
)
array = np.zeros(shape=1, dtype=trade_type)
array['datetime'] = np.datetime64('1970-01-01 00:00:00')
最佳答案
will work if you specify a time unit parameter as dtype='datetime64[s]' on your array creation.
这是工作代码:
trade_type = np.dtype(
[
('id', 'U'),
('timestamp', 'u8'),
('datetime', np.dtype('datetime64[s]'))
]
)
array = np.zeros(shape=1, dtype=trade_type)
array['datetime'] = np.datetime64('1970-01-01 00:00:00')
输出:
希望我理解正确。
关于python - 如何将日期时间列添加到 np.array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853489/