Quantcast
Viewing all articles
Browse latest Browse all 14040

Snap to the nearest or previous datetime point value every interval

I am hoping to downsample the dataset with uneven sub-hour intervals to 6-hourly, with fixed time points of 00H, 6H, 12H and 18H. If data at these time points exist, keep. If not, use the observation from the nearest previous datapoint without data manipulation, keeping original records.

import pandas as pddata = pd.DataFrame({'dt': ['2022-10-29 18:00:00', '2022-10-30 00:00:00','2022-10-30 06:00:00', '2022-10-30 12:00:00','2022-10-30 18:00:00', '2022-10-31 00:00:00','2022-10-31 06:00:00', '2022-10-31 12:00:00','2022-10-31 15:00:00', '2022-10-31 15:30:00','2022-10-31 16:00:00', '2022-10-31 16:30:00','2022-10-31 17:00:00', '2022-10-31 17:30:00','2022-10-31 18:00:00', '2022-10-31 18:30:00','2022-10-31 19:00:00', '2022-10-31 19:30:00','2022-10-31 20:00:00', '2022-10-31 20:30:00','2022-10-31 21:00:00', '2022-10-31 21:30:00','2022-10-31 22:00:00', '2022-10-31 22:30:00','2022-10-31 23:00:00', '2022-10-31 23:30:00','2022-11-01 00:00:00', '2022-11-01 00:30:00','2022-11-01 01:00:00', '2022-11-01 01:30:00','2022-11-01 02:00:00', '2022-11-01 02:30:00','2022-11-01 03:00:00', '2022-11-01 03:30:00','2022-11-01 04:00:00', '2022-11-01 04:30:00','2022-11-01 05:00:00', '2022-11-01 05:30:00','2022-11-01 06:00:00', '2022-11-01 06:30:00','2022-11-01 07:00:00', '2022-11-01 07:30:00','2022-11-01 08:00:00', '2022-11-01 08:30:00','2022-11-01 09:00:00', '2022-11-01 09:30:00','2022-11-01 10:00:00', '2022-11-01 10:30:00','2022-11-01 11:00:00', '2022-11-01 11:30:00'],'Value': [ 8786.8,    8789.5,    8787.7,    8789.2,    8789.4,    8790.3,    8789.6,    8790.5,    8789.7,    8790.6,    8790.5,    8790.5,    8790.4,    8790.4,    8790.3,    8790.2,    8790.1,    8790.0,    8789.9,    8789.8,    8789.7,    8789.5,    8789.5,    8789.3,    8789.2,    8789.6,    8789.8,    8789.8,    8789.7,    8789.6,    8789.7,    8789.5,    8789.5,    8789.3,    8789.2,    8789.6,    8789.8,    8789.8,    8789.7,    8789.6,    8789.7,    8789.6,    8789.5,    8789.3,    8789.2,    8789.6,    8789.8,    8789.8,    8789.7,    8789.6]})data.index = pd.DatetimeIndex(data['dt'])

But with data.resample('6H').last(), strangely I get one data point at 17:30 instead of 18:00 starting at 2022-10-31:

Image may be NSFW.
Clik here to view.
enter image description here


Viewing all articles
Browse latest Browse all 14040

Trending Articles