如何获取这两个时间戳之间的毫秒差(整数)?
到目前为止我的尝试:
import pandas as pd
from datetime import datetime
ts1 = datetime(2018, 1, 1, 22, 36, 9, 38000)
ts2 = datetime(2018, 1, 1, 22, 36, 7, 908000)
df = pd.DataFrame("ts1": [ts1],"ts2": [ts2]})
df['interval_ts'] = df['ts1'] - df['ts2']
df['interval_ts'] = df['interval_ts'].apply(lambda x: x.microseconds / 1000)
我的预期输出是:1130
答案 0 :(得分:1)
使用total_seconds
,乘以1000
并转换为整数:
df['interval_ts'] = ((df['ts1'] - df['ts2']).dt.total_seconds() * 10**3).astype(int)
或转换为本地numpy格式并除以10**6
:
df['interval_ts1'] = ((df['ts1'] - df['ts2']).astype(np.int64) / 10**6).astype(int)
print (df)
ts1 ts2 interval_ts interval_ts1
0 2018-01-01 22:36:09.038 2018-01-01 22:36:07.908 1130 1130