Python Network Hours

huangapple go评论99阅读模式
英文:

Python Network Hours

问题

以下是代码部分的翻译:

  1. def business_hours(start, end):
  2. start = pd.Series(start)
  3. end = pd.Series(end)
  4. mask = (pd.notnull(start) & pd.notnull(end)) & (start.dt.hour >= 9) & (end.dt.hour <= 17) & (start.dt.weekday < 5) & (end.dt.weekday < 5)
  5. result = np.empty(len(start), dtype=float)
  6. result.fill(np.nan)
  7. result[mask] = np.where((start[mask].dt.hour >= 9) & (end[mask].dt.hour <= 17), (end[mask] - start[mask]).astype('timedelta64[h]').astype(float), 0)
  8. return result

希望这能帮助你解决问题。

英文:

Really struggling with this one so any help would be much appreciated.

GOAL - workout the hours between two datetime columns excluding weekends and only taking the hours between the working times of 9 & 17.

Now I have reused a function that I use for network days but the output is wrong and I can't seem to figure out how to get it working.

As an example I have In my data a start date and end date that are as follows

> Start_Date = 2017-07-11 19:33:00
> End_Date = 2017/07/12 12:01:00

and the output I'm after is

>3.02

However the function I do have is returning 16!

Function below -

  1. start = pd.Series(start)
  2. end = pd.Series(end)
  3. mask = (pd.notnull(start) &amp; pd.notnull(end)) &amp; (start.dt.hour &gt;= 9) &amp; (end.dt.hour &lt;= 17) &amp; (start.dt.weekday &lt; 5) &amp; (end.dt.weekday &lt; 5)
  4. result = np.empty(len(start), dtype=float)
  5. result.fill(np.nan)
  6. result[mask] = np.where((start[mask].dt.hour &gt;= 9) &amp; (end[mask].dt.hour &lt;= 17), (end[mask] - start[mask]).astype(&#39;timedelta64[h]&#39;).astype(float), 0)
  7. return result ```
  8. </details>
  9. # 答案1
  10. **得分**: 0
  11. 看起来你需要的是 `businesstimedelta`
  12. ```python
  13. import datetime
  14. import businesstimedelta
  15. start = datetime.datetime.strptime("2017-07-11 19:33:00", "%Y-%m-%d %H:%M:%S")
  16. end = datetime.datetime.strptime("2017-07-12 12:01:00", "%Y-%m-%d %H:%M:%S")
  17. # 定义一个工作日规则
  18. workday = businesstimedelta.WorkDayRule(
  19. start_time=datetime.time(9),
  20. end_time=datetime.time(17),
  21. working_days=[0, 1, 2, 3, 4])
  22. businesshours = businesstimedelta.Rules([workday])
  23. # 计算差异
  24. diff = businesshours.difference(start, end)
  25. print(diff)

输出:

  1. <BusinessTimeDelta 3 hours 60 seconds>

https://pypi.org/project/businesstimedelta/

英文:

It looks like what you need is businesstimedelta

  1. import datetime
  2. import businesstimedelta
  3. start = datetime.datetime.strptime(&quot;2017-07-11 19:33:00&quot;, &quot;%Y-%m-%d %H:%M:%S&quot;)
  4. end = datetime.datetime.strptime(&quot;2017-07-12 12:01:00&quot;, &quot;%Y-%m-%d %H:%M:%S&quot;)
  5. # Define a working day rule
  6. workday = businesstimedelta.WorkDayRule(
  7. start_time=datetime.time(9),
  8. end_time=datetime.time(17),
  9. working_days=[0, 1, 2, 3, 4])
  10. businesshours = businesstimedelta.Rules([workday])
  11. # Calculate the difference
  12. diff = businesshours.difference(start, end)
  13. print(diff)

Output:

  1. &lt;BusinessTimeDelta 3 hours 60 seconds&gt;

https://pypi.org/project/businesstimedelta/

答案2

得分: 0

以下是代码的翻译部分:

  1. # 导入必要的库
  2. import datetime
  3. import businesstimedelta
  4. # 定义工作日规则
  5. workday = businesstimedelta.WorkDayRule(
  6. start_time=datetime.time(9),
  7. end_time=datetime.time(17),
  8. working_days=[0, 1, 2, 3, 4])
  9. # 结合两者
  10. businesshrs = businesstimedelta.Rules([workday])
  11. # 定义一个函数,将时间差转换为分钟
  12. def business_Mins(df, start, end):
  13. try:
  14. # 创建一个布尔掩码,用于选择非空的时间戳
  15. mask = pd.notnull(df[start]) & pd.notnull(df[end])
  16. result = np.empty(len(df), dtype=object)
  17. # 对每一行应用工作日时间差,并将小时转换为分钟
  18. result[mask] = df.loc[mask].apply(lambda x: businesshrs.difference(x[start],x[end]).hours, axis=1)
  19. result[~mask] = np.nan
  20. return result * 60
  21. except KeyError as e:
  22. print(f"错误:在数据框中找不到一个或多个列 - {e}")
  23. return None
  24. # 调用函数,将结果存储在名为'Contact_SLA'的列中
  25. df['Contact_SLA'] = business_Mins(df, 'Date and Time of Instruction', 'Date and Time of Attempted Contact')

请注意,这是给定代码的翻译,包括导入库、定义工作日规则、定义函数以及在数据框中应用函数的部分。

英文:

So, I really struggled finding out how to apply the above into a function but finally after much banging of the head came up with the below. Sharing for the next person in my situation. I wanted to convert to minutes so if not required just remove the *60 at the return

  1. import datetime
  2. import businesstimedelta
  3. # Define a working day
  4. workday = businesstimedelta.WorkDayRule(
  5. start_time=datetime.time(9),
  6. end_time=datetime.time(17),
  7. working_days=[0, 1, 2, 3, 4])
  8. # Combine the two
  9. businesshrs = businesstimedelta.Rules([workday])
  10. def business_Mins(df, start, end):
  11. try:
  12. mask = pd.notnull(df[start]) &amp; pd.notnull(df[end])
  13. result = np.empty(len(df), dtype=object)
  14. result[mask] = df.loc[mask].apply(lambda x: businesshrs.difference(x[start],x[end]).hours, axis=1)
  15. result[~mask] = np.nan
  16. return result * 60
  17. except KeyError as e:
  18. print(f&quot;Error: One or more columns not found in the dataframe - {e}&quot;)
  19. return None
  20. df[&#39;Contact_SLA&#39;] = business_Mins(df, &#39;Date and Time of Instruction&#39;, &#39;Date and Time of Attempted Contact&#39;)

huangapple
  • 本文由 发表于 2023年2月8日 21:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定