[Answered ]-Django TruncHour + annotate Count not inlcuding hours with zero rows

1๐Ÿ‘

โœ…

I wasnโ€™t able to solve this via ORM, but it was trivial to pad the data out in regular Python code.

def time_series_pad_zeros(time_series):
    result = []
    for i in range(0,24):
        found = False
        for data in time_series:
            if data['hour'].hour == i:
                found = True
                result.append({ "hour": i, "event_count": data['event_count'] })

        if not found:
            result.append({ "hour": i, "event_count": 0 })

    return result

Leave a comment