查找两行之间经过了多少时间(数据时间)

问题描述 投票:0回答:2
Subject.iloc[1, 0]  is equal to 

datetime.time(13, 16, 14, 336000)

Subject.iloc[2, 0]  is equal to 

datetime.time(13, 16, 14, 338000)

我要做的就是找到从Subject.iloc [1,0]到Subject.iloc [2,0]的经过时间。但是当我减去它们时,它会说

Subject.iloc[1, 0]-Subject.iloc[2, 0]
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

但是后来我想也许我应该使用时间增量

  pd.to_timedelta(Subject.iloc[2, 0].astype(str))

其中AttributeError:“ datetime.time”对象没有属性“ astype”

有人可以帮我吗?我做错了吗?

甚至这个

pd.to_timedelta(Subject.iloc[2, 0]) won't work
python pandas timestamp timedelta
2个回答
1
投票

您可以尝试这样做

d1 = datetime.datetime.combine(datetime.date.today(), Subject.iloc[2, 0])
d2 = datetime.datetime.combine(datetime.date.today(), Subject.iloc[1, 0])
diff = d1 - d2

要使用熊猫的to_timedelta,请执行以下操作

pd.to_timedelta(str(Subject.iloc[2, 0]))

1
投票

我最近实际上必须为此工作。您可以将日期时间(或本例中的时间结构对象)转换为“自纪元以来的秒数”,然后进行比较。

In Python, how do you convert a `datetime` object to seconds?

对我来说,这更复杂:

durSec = time.mktime(time.strptime(data[7], "%Y-%m-%d %H:%M:%S")) - time.mktime(time.strptime(data[6], "%Y-%m-%d %H:%M:%S"))
© www.soinside.com 2019 - 2024. All rights reserved.