python中有没有一种方法可以减去时间戳?

问题描述 投票:0回答:1

我有带有两个列的数据框。他们两个都是时间戳,但是一路走到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
python pandas datetime time series
1个回答
0
投票

您可以使用,如@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

© www.soinside.com 2019 - 2024. All rights reserved.