如何将 datetime64 数组转换为 int?

9 python numpy pandas

有了这个

pd.Timestamp("31.12.1999 23:59:12").value
>>946684752000000000
Run Code Online (Sandbox Code Playgroud)

我可以获得日期时间基本值的整数值。

如何为一组日期时间值完成此操作?

df = pd.DataFrame({"a": ["31.12.1999 23:59:12", "31.12.1999 23:59:13", "31.12.1999 23:59:14"], "b": [4, 5, 6]})
df.insert(0, 'date', df.a.apply(lambda x: datetime.datetime.strptime(x, "%d.%m.%Y %H:%M:%S")))
Run Code Online (Sandbox Code Playgroud)

显然,这不起作用:

df.date.values.value
>>AttributeError: 'numpy.ndarray' object has no attribute 'value'
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 9

用于to_datetime转换为np.int64

df['int'] = pd.to_datetime(df['a']).astype(np.int64)

print (df)
                     a  b                 int
0  31.12.1999 23:59:12  4  946684752000000000
1  31.12.1999 23:59:13  5  946684753000000000
2  31.12.1999 23:59:14  6  946684754000000000
Run Code Online (Sandbox Code Playgroud)