Related Articles

# Python – Group dates in K ranges

• Last Updated : 03 Aug, 2021

Given a list of dates, group the dates in a successive day ranges from the initial date of the list. We will form a group of each successive range of K dates, starting from the smallest date.

Input : test_list = [datetime(2020, 1, 4), datetime(2019, 12, 30), datetime(2020, 1, 7), datetime(2019, 12, 27), datetime(2020, 1, 20), datetime(2020, 1, 10)], K = 10

Output : [(0, [datetime.datetime(2019, 12, 27, 0, 0), datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)]), (1, [datetime.datetime(2020, 1, 7, 0, 0), datetime.datetime(2020, 1, 10, 0, 0)]), (2, [datetime.datetime(2020, 1, 20, 0, 0)])]

Explanation : 27 Dec – 4 Jan is in same group as diff. of dates are less than 10, successivly, each set of dates are grouped by 10 dayes delta.

Input : test_list = [datetime(2020, 1, 4), datetime(2019, 12, 30), datetime(2020, 1, 7), datetime(2019, 12, 27), datetime(2020, 1, 20), datetime(2020, 1, 10)], K = 14

Output : [(0, [datetime.datetime(2019, 12, 27, 0, 0), datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2020, 1, 4, 0, 0), datetime.datetime(2020, 1, 7, 0, 0)]), (1, [datetime.datetime(2020, 1, 10, 0, 0), datetime.datetime(2020, 1, 20, 0, 0)])]

Explanation : 27 Dec – 7 Jan is in same group as diff. of dates are less than 14, successivly, each set of dates are grouped by 14 dayes delta.

Method : Using groupby() + sort()

In this, we sort the dates and then perform grouping of a set of dates depending upon grouping function.

## Python3

 `# Python3 code to demonstrate working of``# Group dates in K ranges``# Using groupby() + sort()``from` `itertools ``import` `groupby``from` `datetime ``import` `datetime`` ` `# initializing list``test_list ``=` `[datetime(``2020``, ``1``, ``4``),``             ``datetime(``2019``, ``12``, ``30``), ``             ``datetime(``2020``, ``1``, ``7``),``             ``datetime(``2019``, ``12``, ``27``),``             ``datetime(``2020``, ``1``, ``20``), ``             ``datetime(``2020``, ``1``, ``10``)]``              ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# initializing K ``K ``=` `7`` ` `# initializing start date ``min_date ``=` `min``(test_list)`` ` `# utility fnc to form groupings``def` `group_util(date):``    ``return` `(date``-``min_date).days ``/``/` `K`` ` `# sorting before grouping``test_list.sort()`` ` `temp ``=` `[]``# grouping by utility function to group by K days``for` `key, val ``in` `groupby(test_list , key ``=` `lambda` `date : group_util(date)):``    ``temp.append((key, ``list``(val)))`` ` `# using strftime to convert to userfriendly``# format``res ``=` `[]``for` `sub ``in` `temp:``  ``intr ``=` `[]``  ``for` `ele ``in` `sub[``1``]:``    ``intr.append(ele.strftime(``"%Y/%m/%d"``))``  ``res.append((sub[``0``], intr))``     ` `# printing result``print``(``"Grouped Digits : "` `+` `str``(res))`

Output:

The original list is : [datetime.datetime(2020, 1, 4, 0, 0), datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2020, 1, 7, 0, 0), datetime.datetime(2019, 12, 27, 0, 0), datetime.datetime(2020, 1, 20, 0, 0), datetime.datetime(2020, 1, 10, 0, 0)]

Grouped Digits : [(0, [‘2019/12/27’, ‘2019/12/30’]), (1, [‘2020/01/04’, ‘2020/01/07’]), (2, [‘2020/01/10’]), (3, [‘2020/01/20’])]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up