Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to compute date ranges from a list of dates?

I have a list of dates, for example:

['2011-02-27', '2011-02-28', '2011-03-01', '2011-04-12', '2011-04-13', '2011-06-08']

How do I find the contiguous date ranges contained within those dates? In the above example, the ranges should be:

[{"start_date": '2011-02-27', "end_date": '2011-03-01'},
 {"start_date": '2011-04-12', "end_date": '2011-04-13'},
 {"start_date": '2011-06-08', "end_date": '2011-06-08'}
]

Thanks.

like image 290
Continuation Avatar asked Aug 03 '11 22:08

Continuation


People also ask

How do I get a list of dates between two dates in Python?

import pandas from datetime import datetime, timedelta startDate = datetime(2022, 6, 1) endDate = datetime(2022, 6, 10) # Getting List of Days using pandas datesRange = pandas. date_range(startDate,endDate-timedelta(days=1),freq='d') print(datesRange);

How can I get a list of dates between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.


1 Answers

This works, but I'm not happy with it, will work on a cleaner solution an edit the answer. Done, here is a clean, working solution:

import datetime
import pprint

def parse(date):
    return datetime.date(*[int(i) for i in date.split('-')])

def get_ranges(dates):
    while dates:
        end = 1
        try:
            while dates[end] - dates[end - 1] == datetime.timedelta(days=1):
                end += 1
        except IndexError:
            pass

        yield {
            'start-date': dates[0],
            'end-date': dates[end-1]
        }
        dates = dates[end:]

dates = [
    '2011-02-27', '2011-02-28', '2011-03-01',
    '2011-04-12', '2011-04-13',
    '2011-06-08'
]

# Parse each date and convert it to a date object. Also ensure the dates
# are sorted, you can remove 'sorted' if you don't need it
dates = sorted([parse(d) for d in dates]) 

pprint.pprint(list(get_ranges(dates)))

And the relative output:

[{'end-date': datetime.date(2011, 3, 1),
  'start-date': datetime.date(2011, 2, 27)},
 {'end-date': datetime.date(2011, 4, 13),
  'start-date': datetime.date(2011, 4, 12)},
 {'end-date': datetime.date(2011, 6, 8),
  'start-date': datetime.date(2011, 6, 8)}]
like image 107
GaretJax Avatar answered Sep 23 '22 11:09

GaretJax