我收到此错误时间数据“27:07.5”与格式“%H:%M:%S”不匹配(匹配)

2024-03-29 05:45:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出此数据帧中的开始时间和最后时间(分钟)之间的差异:

StartTime     LastTime  
  1  00:02:05    00:02:05
  2  00:07:05    00:07:05
  3  00:12:06    00:12:06
  4  00:17:06   00:17:06 

当我对数据运行以下代码时

from datetime import datetime

date_format = "%H:%M.%S"

# You could also pass datetime.time object in this part and convert it to string.
time_start = str(UDP_interval['StartTime']) 
time_end = str(UDP_interval['LastTime'])

# Then get the difference here.    
diff = datetime.strptime(time_end, date_format) - 
datetime.strptime(time_start, date_format)

# Get the time in hours i.e. 9.60, 8.5
result = diff.seconds / 3600;

我得到这个错误:

dtype: object' does not match format '%H:%M:%S'


Tags: 数据informatdatetimedateobjecttime时间
1条回答
网友
1楼 · 发布于 2024-03-29 05:45:15

你也许应该使用:

t_start = list(map(int, str(UDP_interval['StartTime']).split(':'))) # get hours, minutes, seconds separately
t_end = list(map(int, str(UDP_interval['LastTime']).split(':')))
diff = (t_start[0] - t_end[0]) + (t_start[1] - t_start[1]) / 60 + (t_start[2] - t_start[2]) / 3600

相关问题 更多 >