【问题标题】:How to know in python if a data frame with date format is included within a time interval (Vectorizing)如何在python中知道时间间隔内是否包含日期格式的数据框(矢量化)
【发布时间】:2016-11-23 15:14:33
【问题描述】:

我有以下数据框:

index       arrivalTime
0   01/11/2016 06:53:15 
1   01/11/2016 06:55:25 
2   03/11/2016 16:57:10
3   03/11/2016 16:57:30 
4   04/11/2016 18:58:10
5   06/11/2016 19:58:35 
6   08/11/2016 20:00:05 

我需要知道不同日期 16:00:00 到 20:00:00 之间的行。我举个例子结果:

index       arrivalTime result
0   01/11/2016 06:53:15      0
1   01/11/2016 06:55:25      0
2   03/11/2016 16:57:10      1 
3   03/11/2016 16:57:30      1
4   04/11/2016 18:58:10      1
5   06/11/2016 19:58:35      1
6   08/11/2016 20:00:05      0

我使用 apply 函数和一个嵌套函数得到了上述结果,但速度很慢,我想“矢量化”,但我做不到。

def function_time(df):

    df['hora_lle'] = df['arrivalTime'].map(lambda x: x[-8:])

    def class(hora_lle):

        x = dt.datetime.strptime(hora_lle, '%H:%M:%S').time()
        h1 = dt.datetime.strptime('16:00:00', '%H:%M:%S').time()
        h2 = dt.datetime.strptime('20:00:00', '%H:%M:%S').time()

        if x < h1:
            return 0
        elif h1 <= x < h2:
            return 1
        elif h2 <= x:
            return 0

    df['seg'] = df['hora_sal'].apply(class) 
    return df

非常感谢您

【问题讨论】:

    标签: python pandas time dataframe vectorization


    【解决方案1】:

    我认为你可以先使用to_datetime,然后创建mask,由astype转换为int,但是如果minutesseconds0,则此解决方案不起作用:

    df.arrivalTime = pd.to_datetime(df.arrivalTime) 
    h = df.arrivalTime.dt.hour  
    mask = (h > 15) & (h < 20)
    df['result'] = mask.astype(int)
    
    print (df)
       index         arrivalTime  result
    0      0 2016-01-11 06:53:15       0
    1      1 2016-01-11 06:55:25       0
    2      2 2016-03-11 16:57:10       1
    3      3 2016-03-11 16:57:30       1
    4      4 2016-04-11 18:58:10       1
    5      5 2016-06-11 19:58:35       1
    6      6 2016-08-11 20:00:05       0
    

    dt.time 的类似解决方案 - 提取时间然后比较它们:

    df.arrivalTime = pd.to_datetime(df.arrivalTime)   
    
    h1 = dt.datetime.strptime('16:00:00', '%H:%M:%S').time()
    h2 = dt.datetime.strptime('20:00:00', '%H:%M:%S').time()
    times = df.arrivalTime.dt.time
    mask = (times >= h1) & (times <= h2)
    df['result'] = mask.astype(int)
    print (df)
              arrivalTime  result
    0 2016-01-11 06:53:15       0
    1 2016-01-11 06:55:25       0
    2 2016-03-11 16:57:10       1
    3 2016-03-11 16:57:30       1
    4 2016-04-11 18:58:10       1
    5 2016-06-11 19:58:35       1
    6 2016-08-11 20:00:05       0
    

    另一个使用between_time 的解决方案仅适用于DatetimeIndex - 输出分配给列result 并针对notnull 进行测试:

    df.arrivalTime = pd.to_datetime(df.arrivalTime) 
    df['result'] = 1
    df.set_index('arrivalTime', inplace=True)
    df['result'] = df.between_time('16:00', '20:00')
    df['result'] = df['result'].notnull().astype(int)
    print (df)
                         result
    arrivalTime                
    2016-01-11 06:53:15       0
    2016-01-11 06:55:25       0
    2016-03-11 16:57:10       1
    2016-03-11 16:57:30       1
    2016-04-11 18:58:10       1
    2016-06-11 19:58:35       1
    2016-08-11 20:00:05       0
    

    【讨论】:

    • 一如既往的好哈哈!
    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多