|
20鱼币
本帖最后由 任申猴 于 2022-7-19 15:06 编辑
计算设备使用时间
[2022-07-01 18:31:57\n', '20220701-KBW.S 开始\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:54\n', '2022-07-01 18:23:02\n', '2022-07-01 18:22:35\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:19:38\n', '2022-07-01 09:33:41\n', '2022-07-01 09:33:39\n', '2022-07-01 09:33:30\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:20\n', '2022-07-01 09:33:03\n', '2022-07-01 09:32:59\n', '2022-07-01 09:32:26\n', '2022-07-01 09:31:18\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:13\n', '2022-07-01 09:31:13\n', '2022-07-01 09:22:15\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 03:14:02\n', '2022-07-01 03:14:01\n', '2022-07-01 03:14:01\n', '20220628-kbw.S 完成\n', '2022-07-01 03:14:01\n', '2022-06-30 18:09:17\n', '20220628-kbw.S 开始\n', '2022-06-30 18:09:17\n', '2022-06-30 18:09:16\n', '2022-06-30 18:09:16\n', '2022-06-30 14:48:26\n', '2022-06-30 14:48:26\n', '2022-06-30 14:46:37\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:45:59\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 10:57:16\n', '2022-06-30 10:57:16\n', '2022-06-30 10:57:15\n', '2022-06-30 10:57:15\n', '2022-06-30 10:57:11\n', '2022-06-30 10:35:56\n', '2022-06-30 10:30:55\n', '2022-06-30 10:30:55\n', '2022-06-30 10:16:54\n', '2022-06-30 10:15:30\n', '2022-06-30 10:15:30\n', '2022-06-30 10:15:28\n', '2022-06-30 10:15:28\n', '2022-06-30 10:12:51\n', '2022-06-30 10:11:53\n', '2022-06-30 10:11:53\n', '2022-06-30 10:11:45\n', '2022-06-30 10:11:45\n', '2022-06-30 10:11:44\n', '2022-06-30 10:08:16\n', '2022-06-30 10:08:16\n', '2022-06-30 10:07:12\n', '2022-06-30 10:07:09\n', '2022-06-30 10:06:34\n', '2022-06-30 10:06:34\n', '2022-06-30 10:06:34\n', '20220628-kbw.S 完成\n', '2022-06-30 10:06:34\n', '20220628-kbw.S 开始\n', '2022-06-29 20:36:31\n', '2022-06-29 20:36:30\n', '2022-06-29 20:36:30\n', '2022-06-29 19:46:58\n', '2022-06-29 19:44:50\n', '2022-06-29 19:44:46\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:42:00\n', '2022-06-29 19:41:58\n', '2022-06-29 19:41:48\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:36\n', '2022-06-29 19:41:17\n', '2022-06-29 19:41:12\n', '2022-06-29 19:41:04\n', '2022-06-29 19:40:57\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:47\n', '2022-06-29 19:40:47\n', '2022-06-29 19:40:46\n', '2022-06-29 19:40:43\n', '2022-06-29 19:40:42\n', '2022-06-29 19:38:54\n', '2022-06-29 19:38:53\n', '2022-06-29 19:38:53\n', '20220628-kbw.S 暂停\n', '2022-06-28 22:00:28\n', '20220628-kbw.S 开始\n', '2022-06-28 22:00:28\n', '2022-06-28 22:00:27\n', '2022-06-28 22:00:27\n', '2022-06-28 21:51:38\n', '2022-06-28 21:51:31\n', '2022-06-28 21:51:15\n', '20220627.S\n', '2022-06-28 21:51:13\n', '2022-06-28 21:51:03\n', '2022-06-28 21:51:01\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:52\n', '2022-06-28 21:49:33\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:22:13\n', '20220627 \n', '2022-06-28 21:22:13\n', '20220627.S\n', '2022-06-28 21:22:12\n', '20220627.S 完成\n', '2022-06-28 21:22:12\n', '20220627.LOG\n', '2022-06-28 20:36:11\n', '20220627.S 开始\n', '2022-06-28 20:36:11\n', '20220627 \n', '2022-06-28 20:36:11\n', '20220627.S\n', '2022-06-28 20:36:11\n', '20220627 \n', '2022-06-28 20:35:26\n', '2022-06-28 20:33:52\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:06:03\n', '20220627 \n', '2022-06-28 20:06:03\n', '20220627.S\n', '2022-06-28 20:06:02\n', '20220627.LOG\n', '2022-06-28 20:06:02\n', '20220627.S 完成\n', '2022-06-28 10:35:30\n']
设备使用的总时间 = (暂停时间 - 开始时间) + (完成时间-开始时间)
-kbw.S 开始 到 -kbw.S 暂停 算一个时间段 -kbw.S 开始 到 -kbw.S 完成 是一个时间段 ,暂停后也算完成,下一次使用又是设备开始
情况1 是 开始使用到暂停使用 包含 '-kbw.S 开始\n’ 的前一个时间数据 (ps:红色是设备开始使用的时间) '2022-06-30 18:09:17\n', '20220628-kbw.S 开始\n', 设备暂时的时间 (ps:红色是设备暂停的时间) '2022-06-29 19:38:53\n', '20220628-kbw.S 暂停\n' 计算这个时间差
情况2 是 开始使用到完成使用 '2022-06-30 10:06:34\n', '20220628-kbw.S 完成\n', '2022-06-30 10:06:34\n', '20220628-kbw.S 开始\
需要的时间都是包含“2022-**-** -kbw.S 开始/2022-**-** -kbw.S 暂停/2022-**-** -kbw.S 完成”的前一个日期时间
问题是我需要的时间跟不需要的时间格式都是一样的 我不知道该怎么提取出来
本帖最后由 阿奇_o 于 2022-7-20 00:23 编辑
应该理解你的意思了,给你看看效果(Jupyter Notebook): log = ['2022-07-01 18:31:57\n', '20220701-KBW.S 开始\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:56\n', '2022-07-01 18:31:54\n', '2022-07-01 18:23:02\n', '2022-07-01 18:22:35\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:21:24\n', '2022-07-01 18:19:38\n', '2022-07-01 09:33:41\n', '2022-07-01 09:33:39\n', '2022-07-01 09:33:30\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:26\n', '2022-07-01 09:33:20\n', '2022-07-01 09:33:03\n', '2022-07-01 09:32:59\n', '2022-07-01 09:32:26\n', '2022-07-01 09:31:18\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:16\n', '2022-07-01 09:31:13\n', '2022-07-01 09:31:13\n', '2022-07-01 09:22:15\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 09:21:30\n', '2022-07-01 03:14:02\n', '2022-07-01 03:14:01\n', '2022-07-01 03:14:01\n', '20220628-kbw.S 完成\n', '2022-07-01 03:14:01\n', '2022-06-30 18:09:17\n', '20220628-kbw.S 开始\n', '2022-06-30 18:09:17\n', '2022-06-30 18:09:16\n', '2022-06-30 18:09:16\n', '2022-06-30 14:48:26\n', '2022-06-30 14:48:26\n', '2022-06-30 14:46:37\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:46:01\n', '2022-06-30 14:45:59\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 13:24:35\n', '2022-06-30 10:57:16\n', '2022-06-30 10:57:16\n', '2022-06-30 10:57:15\n', '2022-06-30 10:57:15\n', '2022-06-30 10:57:11\n', '2022-06-30 10:35:56\n', '2022-06-30 10:30:55\n', '2022-06-30 10:30:55\n', '2022-06-30 10:16:54\n', '2022-06-30 10:15:30\n', '2022-06-30 10:15:30\n', '2022-06-30 10:15:28\n', '2022-06-30 10:15:28\n', '2022-06-30 10:12:51\n', '2022-06-30 10:11:53\n', '2022-06-30 10:11:53\n', '2022-06-30 10:11:45\n', '2022-06-30 10:11:45\n', '2022-06-30 10:11:44\n', '2022-06-30 10:08:16\n', '2022-06-30 10:08:16\n', '2022-06-30 10:07:12\n', '2022-06-30 10:07:09\n', '2022-06-30 10:06:34\n', '2022-06-30 10:06:34\n', '2022-06-30 10:06:34\n', '20220628-kbw.S 完成\n',
'2022-06-30 10:06:34\n', '20220628-kbw.S 开始\n', '2022-06-29 20:36:31\n', '2022-06-29 20:36:30\n', '2022-06-29 20:36:30\n', '2022-06-29 19:46:58\n', '2022-06-29 19:44:50\n', '2022-06-29 19:44:46\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:44:11\n', '2022-06-29 19:42:00\n', '2022-06-29 19:41:58\n', '2022-06-29 19:41:48\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:43\n', '2022-06-29 19:41:36\n', '2022-06-29 19:41:17\n', '2022-06-29 19:41:12\n', '2022-06-29 19:41:04\n', '2022-06-29 19:40:57\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:53\n', '2022-06-29 19:40:47\n', '2022-06-29 19:40:47\n', '2022-06-29 19:40:46\n', '2022-06-29 19:40:43\n', '2022-06-29 19:40:42\n', '2022-06-29 19:38:54\n', '2022-06-29 19:38:53\n', '2022-06-29 19:38:53\n', '20220628-kbw.S 暂停\n', '2022-06-28 22:00:28\n', '20220628-kbw.S 开始\n', '2022-06-28 22:00:28\n', '2022-06-28 22:00:27\n', '2022-06-28 22:00:27\n', '2022-06-28 21:51:38\n', '2022-06-28 21:51:31\n', '2022-06-28 21:51:15\n', '20220627.S\n', '2022-06-28 21:51:13\n', '2022-06-28 21:51:03\n', '2022-06-28 21:51:01\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:59\n', '2022-06-28 21:50:52\n', '2022-06-28 21:49:33\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:49:29\n', '2022-06-28 21:22:13\n', '20220627 \n', '2022-06-28 21:22:13\n', '20220627.S\n', '2022-06-28 21:22:12\n', '20220627.S 完成\n', '2022-06-28 21:22:12\n', '20220627.LOG\n', '2022-06-28 20:36:11\n', '20220627.S 开始\n', '2022-06-28 20:36:11\n', '20220627 \n', '2022-06-28 20:36:11\n', '20220627.S\n', '2022-06-28 20:36:11\n', '20220627 \n', '2022-06-28 20:35:26\n', '2022-06-28 20:33:52\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:15:44\n', '2022-06-28 20:06:03\n', '20220627 \n', '2022-06-28 20:06:03\n', '20220627.S\n', '2022-06-28 20:06:02\n', '20220627.LOG\n', '2022-06-28 20:06:02\n', '20220627.S 完成\n', '2022-06-28 10:35:30\n']
import re
the_time = re.findall('(\d+-\d+-\d+ \d+:\d+:\d+)\n\d+-[KBWkbw]+.S (\w+)\n', ''.join(log))
the_time
#---
[('2022-07-01 18:31:57', '开始'),
('2022-07-01 03:14:01', '完成'),
('2022-06-30 18:09:17', '开始'),
('2022-06-30 10:06:34', '完成'),
('2022-06-30 10:06:34', '开始'),
('2022-06-29 19:38:53', '暂停'),
('2022-06-28 22:00:28', '开始')]
#%%
df = pd.DataFrame(the_time, columns=['time', 'flag'])
df = df.astype({'time': 'datetime64'})
df
#%%
df0 = df.sort_values('time').reset_index(drop=True)
df0
#%%
df1 = df.sort_values('time').reset_index(drop=True).shift()
df1
#%%
res = pd.concat([df0, df1], axis=1)
res.columns = ['t1', 'f1', 't2', 'f2']
res
#---
t1 f1 t2 f2
0 2022-06-28 22:00:28 开始 NaT NaN
1 2022-06-29 19:38:53 暂停 2022-06-28 22:00:28 开始
2 2022-06-30 10:06:34 完成 2022-06-29 19:38:53 暂停
3 2022-06-30 10:06:34 开始 2022-06-30 10:06:34 完成
4 2022-06-30 18:09:17 开始 2022-06-30 10:06:34 开始
5 2022-07-01 03:14:01 完成 2022-06-30 18:09:17 开始
6 2022-07-01 18:31:57 开始 2022-07-01 03:14:01 完成
#%%
result = res[ ((res.f1 == '暂停') & (res.f2 == '开始')) |
((res.f1 == '完成') & (res.f2 == '开始')) ].loc[:]
# 计算时间差
result['used_time'] = result.t1 - result.t2
display(result)
#---
t1 f1 t2 f2 used_time
1 2022-06-29 19:38:53 暂停 2022-06-28 22:00:28 开始 0 days 21:38:25
5 2022-07-01 03:14:01 完成 2022-06-30 18:09:17 开始 0 days 09:04:44
# 统计时间(设备使用的总时间)
result.agg({'used_time': sum}, )
#---
used_time 1 days 06:43:09
dtype: timedelta64[ns]
ps: 若对你帮助很大,不妨请我喝杯奶茶,蜜雪冰城的哦 ^_ 【WX号:Archi_ATM】
|
最佳答案
查看完整内容
应该理解你的意思了,给你看看效果(Jupyter Notebook):
ps: 若对你帮助很大,不妨请我喝杯奶茶,蜜雪冰城的哦 ^_ 【WX号:Archi_ATM】
|