将 numpy datetime64 转换为长整数并返回

Inq*_*tor 3 python datetime numpy

如何将 NumPy datetime64 转换为长整数并返回?

import numpy as np
import datetime

np.datetime64(datetime.datetime.now()).astype(long)
Run Code Online (Sandbox Code Playgroud)

给出值 1511975032478959

np.datetime64(np.datetime64(datetime.datetime.now()).astype(long))
Run Code Online (Sandbox Code Playgroud)

给出一个错误:

ValueError: Converting an integer to a NumPy datetime requires a specified unit
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 6

您需要指定 long int 的单位(在本例中为微秒)。

 np.datetime64(np.datetime64(datetime.datetime.now()).astype(long), 'us')
Run Code Online (Sandbox Code Playgroud)

返回

 numpy.datetime64('2017-11-29T17:11:44.638713')
Run Code Online (Sandbox Code Playgroud)