Suppose I have a multi-index pandas dataframe containing datetime intervals
arrays = [[pd.datetime(2022,1,1), pd.datetime(2022,2,1), pd.datetime(2022,1,1), pd.datetime(2022,4,1)], [pd.datetime(2022,1,31), pd.datetime(2022,2,28), pd.datetime(2022,3,30), pd.datetime(2022,4,30)]]
idx = pd.MultiIndex.from_arrays(arrays)
df = pd.DataFrame(np.zeros(4), index = idx)
df
0
2022-01-01 2022-01-31 0.0
2022-02-01 2022-02-28 0.0
2022-01-01 2022-03-30 0.0
2022-04-01 2022-04-30 0.0
What I want to do is to drop the intervals that are completely covered by another interval. In the example, we observe the interval 01.01.2022-30.03.2022, so I want to drop the first two observations.
I tried pandas overlaps(), where I am getting True for overlapping intervals:
iix = pd.IntervalIndex.from_arrays(df.index.get_level_values(0), df.index.get_level_values(1), closed='both')
[iix.overlaps(x)for x in iix]
[array([ True, False, True, False]),
array([False, True, True, False]),
array([ True, True, True, False]),
array([False, False, False, True])]
Based on this, I could drop, but then I would also drop the largest overlapping interval. So not a good solution. Does anybody have a better one? I'd be very grateful!