【问题标题】:is there a way in python to subtract timestamps?python中有没有办法减去时间戳?
【发布时间】:2020-07-21 06:26:25
【问题描述】:

我有两个列的数据框。它们都是时间戳,但是一个一直到 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

【问题讨论】:

  • col1.sub(col2) 应该可以解决问题
  • 并非如此。我有两个 cols 一个一直到 FFF,另一个到第二个。我在几分钟内寻找答案。
  • @CoryKramer 我试过了,我得到了像 -1 天 +23:59:59.875000 这样的值,有没有办法将这些转换为分钟和秒?

标签: python pandas datetime time series


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2021-06-27
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多