如何创建一个空白的datetime对象?所以我可以找到时间增量的平均值

时间:2019-07-18 12:56:11

标签: python-3.x time date-arithmetic

我已经将当前时间戳转换为日期时间对象,并且能够减去它们。我的问题是我需要总结所有增量,但无法创建“空白”日期时间对象。例如0000-00-00 00:00:00.000

    low_sum_time_alert_fired_to_closed = 0
    low_sum_time_investigate_open_to_closed = 0
    low_sum_time_alert_fired_to_investigation = 0
    low_sum_time_alert_fired_to_first_assignment = 0
#  iterate through the row index's
    for idx in range(amt_of_rows):
        # Index the row of the dataframe
        row = df.iloc[idx]
        vendor_name = row[2]
        #  formatting the timestamps
        insert_at = datetime.strptime(row[3], "%Y-%m-%d %H:%M:%S.%f")
        first_assignment = datetime.strptime(row[4], "%Y-%m-%d %H:%M:%S.%f")
        first_investigated = datetime.strptime(row[5], "%Y-%m-%d %H:%M:%S.%f")
        time_closed = datetime.strptime(row[6], "%Y-%m-%d %H:%M:%S.%f")

        if row[0] == "LOW":
            num_low += 1
####### THIS IS WHERE THINGS BREAK SINCE I'M TRYING TO ADD THE DIFFERENCE OF THE TIME STAMPS TO AN INTEGER ###
            low_sum_time_alert_fired_to_closed += time_closed - insert_at 
            low_sum_time_investigate_open_to_closed += time_closed - first_investigated
            low_sum_time_alert_fired_to_investigation += first_investigated - insert_at
            low_sum_time_alert_fired_to_first_assignment += first_assignment - insert_at

    low_sum_time_alert_fired_to_closed = low_sum_time_alert_fired_to_closed/amt_of_rows
    low_sum_time_investigate_open_to_closed = low_sum_time_investigate_open_to_closed/amt_of_rows
    low_sum_time_alert_fired_to_investigation = low_sum_time_alert_fired_to_investigation/amt_of_rows
    low_sum_time_alert_fired_to_first_assignment = low_sum_time_alert_fired_to_first_assignment/amt_of_rows

1 个答案:

答案 0 :(得分:0)

要汇总所有timedelta,您需要从空白的timedelta开始,而不是datetime

仅从datetime.timedelta(0)开始。

简单的例子:

import datetime

td0 = datetime.timedelta(0)

td1 = datetime.timedelta(hours=1, minutes=1, seconds=1)
td2 = datetime.timedelta(hours=2, minutes=2, seconds=2)
td3 = datetime.timedelta(hours=3, minutes=3, seconds=3)

td0 += td1
td0 += td2
td0 += td3

print(td0)

输出:

6:06:06