Say I have a list of lists where each nested list has two values: a start date of a range and an end date of a range. So something like this:
ranges_list = [
['2020-03-12', '2020-06-12'],
['2020-03-13', '2020-06-13'],
['2020-03-14', '2020-06-14']
]
This represents 3 ranges:
- March 12th 2020 - June 12th 2020
- March 13th 2020 - June 13th 2020
- March 14th 2020 - June 14th 2020
Say I also have some dataframe d
which has multiple columns, one of which is a column called 'occurence_date'
that contains datetimes in it.
Say the dataframe d
looks like:
ID LinkID PC occurence_date
10R46 R*1005 8017 2020-03-12
10R46 R*10335 5019 2020-04-15
100R91 R*1005 8017 2020-04-15
10R91 R*243 8870 2020-06-14
I want to group dataframe d
using the occurence_date
column on the ranges specified in ranges_list
So something like:
grouped = d.groupby('occurence_date', ranges=ranges_list)
Obviously, this groupby code is incorrect but helps serve what i want to do.
At the end of it all, the grouped object should have 3 distinct groups that look like:
group: ('2020-03-12', '2020-06-12')
ID LinkID PC occurence_date
10R46 R*1005 8017 2020-03-12
10R46 R*10335 5019 2020-04-15
100R91 R*1005 8017 2020-04-15
group: ('2020-03-13', '2020-06-13')
ID LinkID PC occurence_date
10R46 R*10335 5019 2020-04-15
100R91 R*1005 8017 2020-04-15
group: ('2020-03-14', '2020-06-14')
ID LinkID PC occurence_date
10R46 R*10335 5019 2020-04-15
100R91 R*1005 8017 2020-04-15
10R91 R*243 8870 2020-06-14
How can I go about accomplishing this?