Title: Is there a way to select multiple time invervals in a single xarray dataset
Tags: python-xarray
Question: I have a xarray dataset that contains sensor samples from 9-5-2021 5am to 17-5-2021 12:20pm and the sampling period is 10Hz.
I would like to extract information from
5am-520am, 9am-920am , 1pm-120pm , 4-420pm... (4 hour time between intervals for 20 minutes each).
So far I know that the following code
selects that one time interval:
file_dataset.sel(time = slice('2021-05-09T0:05:00','2021-05-09T0:05:20'))
I sort of want to do this but I know it does not work:
file.sel(time = (slice('2021-05-09T0:05:00','2021-05-09T0:05:20'), slice('2021-05-09T0:09:00','2021-05-09T0:09:20')...slice('2021-05-17T0:12:00','2021-05-17T0:12:20'))
Is there an xarray method that can select from this format?
Thank you
Comment: The _time interval_ `slice('2021-05-09T0:05:00','2021-05-09T0:05:20')` is not within _9-5-2021 5am to 17-5-2021 12:20pm_.

Comment: _1pm-120pm , 4-420pm_ are not _4 hour time_ apart.


Here is another answer: 5am-520am, 9am-920am , 1pm-120pm …4 hour time between intervals for 20 minutes each
You can use xarray.Dataset.where:
file_dataset.where((file_dataset.time.dt.hour%4 == 1) &amp; (file_dataset.time.dt.minute <= 20), drop=True)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:07/29/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python-xarray>', 'Title': 'Is there a way to select multiple time invervals in a single xarray dataset', 'ViewCount': 203, 'id': 68568830, 'user_AboutMe': None, 'user_AccountId': 22328571.0, 'user_DisplayName': 'binverted', 'user_Reputation': 63.0, 'user_Views': 9.0, 'user_id': 16550167.0}}