1

I have a list which contains various nested lists, themselves always holding two arrow objects constituting date ranges from a start date (index 0) to an end date (index 1).

I aim to merge any overlapping lists, then sorting them chronologically.

import arrow

date_range_lst = [
    [arrow.get("2020-01-05"), arrow.get("2020-02-26")],
    [arrow.get("2020-02-07"), arrow.get("2020-02-08")],
    [arrow.get("2020-05-16"), arrow.get("2020-07-28")],
    [arrow.get("2020-02-25"), arrow.get("2020-03-19")],
    [arrow.get("2020-05-15"), arrow.get("2020-05-16")],
    [arrow.get("2021-03-01"), arrow.get("2021-04-01")],
    [arrow.get("2021-05-05"), arrow.get("2021-08-08")],
]

date_range_merged_lst = []

# Code here

print(date_range_merged_lst)

For the above example, the intended output would look like this (done manually, but should be correct):

[[<Arrow [2020-01-05T00:00:00+00:00]>, <Arrow [2020-03-19T00:00:00+00:00]>],
[<Arrow [2020-05-15T00:00:00+00:00]>, <Arrow [2020-07-28T00:00:00+00:00]>],
[<Arrow [2021-03-01T00:00:00+00:00]>, <Arrow [2021-04-01T00:00:00+00:00]>],
[<Arrow [2021-05-05T00:00:00+00:00]>, <Arrow [2021-08-08T00:00:00+00:00]>]]
quadratecode
  • 396
  • 1
  • 2
  • 12

1 Answers1

2

I found a solution by using the merge library by Guybrush. None of the other solutions for similar problems found on this site worked for me (e.g. in this thread or this thread).

import arrow

import portion

def merge(inputs):
    intervals = [portion.closed(a, b) for a, b in inputs]
    merge = portion.Interval(*intervals)
    merge = [[i.lower, i.upper] for i in merge]
    return merge


date_range_lst = [
    [arrow.get("2020-01-05"), arrow.get("2020-02-26")],
    [arrow.get("2020-02-07"), arrow.get("2020-02-08")],
    [arrow.get("2020-05-16"), arrow.get("2020-07-28")],
    [arrow.get("2020-02-25"), arrow.get("2020-03-19")],
    [arrow.get("2020-05-15"), arrow.get("2020-05-16")],
    [arrow.get("2021-03-01"), arrow.get("2021-04-01")],
    [arrow.get("2021-05-05"), arrow.get("2021-08-08")],
]

date_range_lst_merged_manual = [
    [arrow.get("2020-01-05"), arrow.get("2020-03-19")],
    [arrow.get("2020-05-15"), arrow.get("2020-07-28")],
    [arrow.get("2021-03-01"), arrow.get("2021-04-01")],
    [arrow.get("2021-05-05"), arrow.get("2021-08-08")],
]


date_range_lst_merged_auto = merge(date_range_lst)

if date_range_lst_merged_manual == date_range_lst_merged_auto:
    print("Test passed")
else:
    print("Test failed")

Output:

Test passed
quadratecode
  • 396
  • 1
  • 2
  • 12