计算时间间隔

时间:2018-08-16 18:55:32

标签: python pandas

我正在尝试计算车辆在特定时间6:00 AM-18:00 PM和18:00 PM-6:00 AM之间停放的时间。我可以成功计算出车辆从6:00 AM-18:00 PM和18:00 PM-11:59 PM停放的时间。时间过了11:59 PM(至12:00 AM),便无法计算。

车辆CSV:

vehicle    park_start_time     park_end_time      duration    enforce_time_1      enforce_time_2
car1       2/10/2018 14:13      2/10/2018 14:20    7          6:00 to 18:00        18:00 to 6:00
car2       2/2/2018 11:06       2/3/2018 19:17     1931       6:00 to 18:00        18:00 to 6:00 

代码:

import numpy as np
import pandas as pd
df = pd.read_csv('vehicle.csv', sep=';')
test = df
test['Start_Enforce_1'] = test['enforced_time_1'].str.split().str[0].str.split(':').str[0].astype(int)
test['Finish_Enforce_1'] = test['enforced_time_1'].str.split().str[-1].str.split(':').str[0].astype(int)

test['Start_Enforce_2'] = test['enforced_time_2'].str.split().str[0].str.split(':').str[0].astype(int)
test['Finish_Enforce_2'] = test['enforced_time_2'].str.split().str[-1].str.split(':').str[0].astype(int)


def check_time(df_line):
    max_hours = float(df_line['max time'])
    dt_range = pd.date_range(start = df_line['Parked_from'], end=df_line['Parked_to'], freq='1Min')
    return np.sum((dt_range.hour >= (df_line['Start_Enforce_1'])) & (dt_range.hour < df_line['Finish_Enforce_1']))
def check_time_2(df_line):
    max_hours = float(df_line['max time'])
    dt_range = pd.date_range(start = df_line['Parked_from'], end=df_line['Parked_to'], freq='1Min')
    return np.sum((dt_range.hour >= (df_line['Start_Enforce_2'])) & (dt_range.hour < df_line['Finish_Enforce_2']))

test['6am to 18pm'] = test.apply(check_time, axis=1)
test['18pm to 6am'] = test.apply(check_time_2, axis=1)
test.to_csv("result.csv", sep=';', encoding='utf-8', index=False)

结果:

vehicle;park_start_time;park_end_time;duration;enforced_time_1;enforced_time_2;Start_Enforce_1;Finish_Enforce_1;Start_Enforce_2;Finish_Enforce_2;6am to 18pm;18pm to 6am
car1;2/10/2018 14:13;2/10/2018 14:20;7;6:00 to 18:00;18:00 to 6:00;6;18;18;6;8;0
car2;2/2/2018 11:06;2/3/2018 19:17;1931;6:00 to 18:00;18:00 to 6:00;6;18;18;6;1134;0

对于 car2 ,您可以看到 1134分钟的停车时间仅是从上午6点到晚上18点。 关于如何捕获晚上11:59之后剩余时间的任何想法或建议,将不胜感激。

1 个答案:

答案 0 :(得分:1)

在您的代码中,对于check_time_2,如果将&(或)替换为|(或),则它应该会更好,因为小时数小于6或大于18,并且不能都像check_time中那样,必须同时在6以上和18以下。因此您的函数check_time_2应该是:

def check_time_2(df_line):
    max_hours = float(df_line['max time']) # not sure it's necessary actually...
    dt_range = pd.date_range(start = df_line['Parked_from'], end=df_line['Parked_to'], freq='1Min')
    return np.sum((dt_range.hour >= (df_line['Start_Enforce_2'])) | (dt_range.hour < df_line['Finish_Enforce_2']))