Blu*_*oon 2 python datetime dataframe pandas
如何以整数形式获取 pandas 中这两个时间戳之间的毫秒差?
到目前为止我的尝试:
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)
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:1130
使用total_seconds、 乘以1000并强制转换为整数:
df['interval_ts'] = ((df['ts1'] - df['ts2']).dt.total_seconds() * 10**3).astype(int)
Run Code Online (Sandbox Code Playgroud)
或者转换为原生 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2608 次 |
| 最近记录: |