社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python DateTime In和DateTime Out-关于时间窗口的计算

Andrea • 4 年前 • 553 次点击  

我有一个包含两个DateTime对象列的数据帧(一个表示手术打卡,另一个表示手术打卡)。对于每一行(即案例),我需要创建一列营业时间内的总时间(07:00-17:30)和另一列营业时间外的总时间。我不确定最好的方法。

我的数据帧的可复制段:

    Actual Room In DateTime Actual Room Out DateTime
0   2013-11-01 02:16    2013-11-01 04:35
1   2016-06-10 16:42    2016-06-10 19:28
2   2014-12-13 09:15    2014-12-13 10:55
3   2014-01-03 19:46    2014-01-03 22:54
4   2015-01-12 18:13    2015-01-12 19:58
5   2017-03-24 18:55    2017-03-24 19:57
6   2015-08-07 18:46    2015-08-07 19:42
7   2016-03-18 20:43    2016-03-19 00:40
8   2017-02-23 15:21    2017-02-23 17:35
9   2013-11-29 17:08    2013-11-29 17:42
10  2014-05-28 18:17    2014-05-28 19:12
11  2017-07-15 17:04    2017-07-15 18:19
12  2017-02-16 09:14    2017-02-16 21:29
13  2014-07-11 12:04    2014-07-11 17:40
14  2017-07-05 12:27    2017-07-05 20:08
15  2014-08-18 17:55    2014-08-18 19:50
16  2015-01-23 15:41    2015-01-23 19:41
17  2015-01-12 16:59    2015-01-12 17:49
18  2014-02-23 11:24    2014-02-23 15:06
19  2017-09-21 13:40    2017-09-21 18:11

pd.read_clipboard(sep=',')

两列之间的最长时间为:

df['Room Difference'] = df['Actual Room Out DateTime'] - df['Actual Room In DateTime']
max(df['Room Difference'])
Timedelta('1 days 01:17:00')

我想应该是这样的(作为伪代码):

if 00:00:00 <= 'Actual Room In DateTime' < 07:00:00 and 00:00:00 <= 'Actual Room Out DateTime' < 07:00:00:
     'After-hours' = 'Actual Room Out DateTime' - 'Actual Room In DateTime'
... to cover all the possible cases.

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57095
 
553 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Hasan Shaukat
Reply   •   1 楼
Hasan Shaukat    4 年前

从营业开始时间中减去时间,在一天开始前几小时到达外面

从工作结束时间中减去下班时间,得到每天下班后的时间

把手术时间减去手术时间

将这两个外部时间相加,得到总外部时间

为每个计算创建一个单独的列