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

正如@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

最新更新