【问题标题】:python time interval overlap durationpython时间间隔重叠持续时间
【发布时间】:2020-02-28 08:44:34
【问题描述】:

我的问题类似于Efficient date range overlap calculation in python?,但是,我需要使用完整的时间戳而不是天数来计算重叠,但更重要的是,我无法将特定日期指定为重叠,而只能指定几个小时。

import pandas as pd
import numpy as np

df = pd.DataFrame({'first_ts': {0: np.datetime64('2020-01-25 07:30:25.435000'),
  1: np.datetime64('2020-01-25 07:25:00')},
 'last_ts': {0: np.datetime64('2020-01-25 07:30:25.718000'),
  1: np.datetime64('2020-01-25 07:25:00')}})
df['start_hour'] = 7
df['start_minute'] = 0
df['end_hour'] = 8
df['end_minute'] = 0
display(df)

如何计算间隔(first_ts、last_ts)与第二个间隔的重叠持续时间(以毫秒为单位)? 潜在地,我需要在每一天构建一个时间戳,其间隔由小时定义,然后计算重叠。

【问题讨论】:

    标签: python pandas time intervals overlap


    【解决方案1】:

    想法是为开始和结束日期时间创建新系列,日期时间列,使用numpy.minimumnumpy.maximum,通过Series.dt.total_seconds 减去、转换timedeltas 并通过1000 倍增:

    s = (df['first_ts'].dt.strftime('%Y-%m-%d ') + 
         df['start_hour'].astype(str) + ':' + 
         df['start_minute'].astype(str))
    e = (df['last_ts'].dt.strftime('%Y-%m-%d ') + 
         df['end_hour'].astype(str) + ':' +
         df['end_minute'].astype(str))
    
    s = pd.to_datetime(s, format='%Y-%m-%d %H:%M')
    e = pd.to_datetime(e, format='%Y-%m-%d %H:%M')
    
    df['inter'] = ((np.minimum(e, df['last_ts']) - 
                    np.maximum(s, df['first_ts'])).dt.total_seconds() * 1000)
    print (df)
                     first_ts                 last_ts  start_hour  start_minute  \
    0 2020-01-25 07:30:25.435 2020-01-25 07:30:25.718           7             0   
    1 2020-01-25 07:25:00.000 2020-01-25 07:25:00.000           7             0   
    
       end_hour  end_minute  inter  
    0         8           0  283.0  
    1         8           0    0.0  
    

    另一个想法是只使用np.minumum

    df['inter'] = (np.minimum(df['last_ts'] - df['first_ts'], e - s).dt.total_seconds() * 1000)
    print (df)
                     first_ts                 last_ts  start_hour  start_minute  \
    0 2020-01-25 07:30:25.435 2020-01-25 07:30:25.718           7             0   
    1 2020-01-25 07:25:00.000 2020-01-25 07:25:00.000           7             0   
    
       end_hour  end_minute  inter  
    0         8           0  283.0  
    1         8           0    0.0  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多