首页 > 解决方案 > python有没有办法减去时间戳?

问题描述

我有两个列的数据框。它们都是时间戳,但是一个一直到 fff 到秒之外的地方。有没有办法在分钟和秒之间获得差异?

col1            col2
2:21:40.756     2:21:41
2:22:30.343     2:22:31
2:24:43.342     2:24:44

i have tried following:
col1= pd.todatetime(df.col1,format='%H:%M:%S.%f').dt.time
col2 = pd.todatetime(df.col2,format = '%H:%M:%S').dt.time
diff = col1-col2
how ever im getting error -: not supported from datetime.date to datetime.date

标签: pythonpandasdatetimetimeseries

解决方案


正如@CoryKramer 所说,您可以使用.sub:

分钟:

col1.sub(col2).dt.seconds/60

0    1439.983333
1    1439.983333
2    1439.983333
dtype: float64

如果您想要更精确,以微秒为单位:

col1.sub(col2).dt.microseconds

0    756000
1    343000
2    342000
dtype: int64


推荐阅读