python - 如何将日期时间列添加到 np.array

标签 python numpy

我想知道。如何将日期时间列添加到 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')

输出:

enter image description here

enter image description here

希望我理解正确。

引用:Datetimes and Timedeltas

关于python - 如何将日期时间列添加到 np.array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853489/

相关文章:

python - 计算pandas中事件之间的时间差

python - Beautiful Soup 无法从表中获取信息

python - 处理 NaN 进行距离计算时出现问题?

python - 将缺失点的点连接到零

python - 如何使用 python 对单个矩阵值求平方?

python - joblib 与 pickle 的不同用例是什么?

python - 索引错误 : tuple index out of range -- String formatting

python - Django相关模型无法解析

python - 将 numpy 数组作为列添加到 Pandas 数据框

python - 生成一个随机的 3 元素 Numpy 整数数组,总和为 3