'How to obtain all gaps as start .. stop interval in pandas datetime index
I want to find all gaps in pandas DateTime index as a list of intervals. For example:
'2022-05-06 00:01:00'
'2022-05-06 00:02:00' <- Start of gap
'2022-05-06 00:06:00' <- End of gap
'2022-05-06 00:07:00'
'2022-05-06 00:08:00'
'2022-05-06 00:09:00' <- Next gap start
'2022-05-06 05:00:00' <- End
'2022-05-06 05:01:00'
And I whant to get next:
[('2022-05-06 00:03:00', '2022-05-06 00:05:00') ,
('2022-05-06 00:10:00', '2022-05-06 04:59:00')]
The frequency could be any, but the same for all index.
Solution 1:[1]
IIUC you can calculate the diff the identify the gaps. Use a mask to slice the starts and stops, and zip them as list.
# ensure datetime
df['datetime'] = pd.to_datetime(df['datetime'])
# threshold
t = pd.Timedelta('1min')
mask = df['datetime'].diff().gt(t)
# get values
starts = df.loc[mask.shift(-1, fill_value=False), 'datetime'].add(t).astype(str)
stops = df.loc[mask, 'datetime'].sub(t).astype(str)
# build output
out = list(zip(starts, stops))
Output:
[('2022-05-06 00:03:00', '2022-05-06 00:05:00'),
('2022-05-06 00:10:00', '2022-05-06 04:59:00')]
Used input:
datetime
0 2022-05-06 00:01:00
1 2022-05-06 00:02:00
2 2022-05-06 00:06:00
3 2022-05-06 00:07:00
4 2022-05-06 00:08:00
5 2022-05-06 00:09:00
6 2022-05-06 05:00:00
7 2022-05-06 05:01:00
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
