Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split list of datetimes into days

I've got a sorted list of datetimes: (with day gaps)

list_of_dts = [
              datetime.datetime(2012,1,1,0,0,0), 
              datetime.datetime(2012,1,1,1,0,0), 
              datetime.datetime(2012,1,2,0,0,0), 
              datetime.datetime(2012,1,3,0,0,0),
              datetime.datetime(2012,1,5,0,0,0),
              ]

And I'd like to split them in to a list for each day:

result = [
          [datetime.datetime(2012,1,1,0,0,0), datetime.datetime(2012,1,1,1,0,0)],
          [datetime.datetime(2012,1,2,0,0,0)],
          [datetime.datetime(2012,1,3,0,0,0)],
          [], # Empty list for no datetimes on day
          [datetime.datetime(2012,1,5,0,0,0)]
         ]

Algorithmically, it should be possible to achieve at least O(n).

Perhaps something like the following: (This obviously doesn't handle missed days, and drops the last dt, but it's a start)

def dt_to_d(list_of_dts):
    result = []
    start_dt = list_of_dts[0]
    day = [start_dt]
    for i, dt in enumerate(list_of_dts[1:]):
        previous = start_dt if i == 0 else list_of_dts[i-1]
        if dt.day > previous.day or dt.month > previous.month or dt.year > previous.year: 
            # split to new sub-list
            result.append(day)
            day = []
            # Loop for each day gap?
        day.append(dt)
    return result

Thoughts?

like image 524
Alex L Avatar asked Jan 24 '12 07:01

Alex L


2 Answers

The easiest way to go is to use dict.setdefault to group entries falling on the same day and then loop over the lowest day to the highest:

>>> import datetime
>>> list_of_dts = [
              datetime.datetime(2012,1,1,0,0,0),
              datetime.datetime(2012,1,1,1,0,0),
              datetime.datetime(2012,1,2,0,0,0),
              datetime.datetime(2012,1,3,0,0,0),
              datetime.datetime(2012,1,5,0,0,0),
              ]

>>> days = {}
>>> for dt in list_of_dts:
        days.setdefault(dt.toordinal(), []).append(dt)

>>> [days.get(day, []) for day in range(min(days), max(days)+1)]
[[datetime.datetime(2012, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 1, 0)], 
 [datetime.datetime(2012, 1, 2, 0, 0)],
 [datetime.datetime(2012, 1, 3, 0, 0)],
 [],
 [datetime.datetime(2012, 1, 5, 0, 0)]]

Another approach for making such groupings is itertools.groupby. It is designed for this kind of work, but it doesn't provide a way to fill-in an empty list for missing days:

>>> import itertools
>>> [list(group) for k, group in itertools.groupby(list_of_dts,
                                                   key=datetime.datetime.toordinal)]
[[datetime.datetime(2012, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 1, 0)], 
 [datetime.datetime(2012, 1, 2, 0, 0)],
 [datetime.datetime(2012, 1, 3, 0, 0)],
 [datetime.datetime(2012, 1, 5, 0, 0)]]
like image 151
Raymond Hettinger Avatar answered Oct 12 '22 06:10

Raymond Hettinger


You can use itertools.groupby to easily handle this kind of problems:

import datetime
import itertools

list_of_dts = [
        datetime.datetime(2012,1,1,0,0,0), 
        datetime.datetime(2012,1,1,1,0,0), 
        datetime.datetime(2012,1,2,0,0,0), 
        datetime.datetime(2012,1,3,0,0,0),
        datetime.datetime(2012,1,5,0,0,0),
        ]

print [list(g) for k, g in itertools.groupby(list_of_dts, key=lambda d: d.date())]
like image 35
qiao Avatar answered Oct 12 '22 05:10

qiao