Pandas downsampling time series

I have a time series in pandas that looks like this:

2012-01-01 00:00:00.250000 12 2012-01-01 00:00:00.257000 34 2012-01-01 00:00:00.258000 45 2012-01-01 00:00:01.350000 56 2012-01-01 00:00:02.300000 78 2012-01-01 00:00:03.200000 89 2012-01-01 00:00:03.500000 90 2012-01-01 00:00:04.200000 12 

Is there a way to lower it to 1 second of data without aligning to 1 second boundaries? For example, is there a way to get this data (assuming downsampling when using the last value that occurs before or during the sampling):

 2012-01-01 00:00:00.250000 12 2012-01-01 00:00:01.250000 45 2012-01-01 00:00:02.250000 56 2012-01-01 00:00:03.250000 89 2012-01-01 00:00:04.250000 12
2012-01-01 00:00:00.250000 12 2012-01-01 00:00:01.250000 45 2012-01-01 00:00:02.250000 56 2012-01-01 00:00:03.250000 89 2012-01-01 00:00:04.250000 12 
+4
source share
1 answer

Create a DateTimeIndex with a frequency of 1 second and a quarter second offset like this.

 index = pd.date_range('2012-01-01 00:00:00.25', '2012-01-01 00:00:04.25', freq='S') 

Compare your data with this index and "fill in ahead" to reduce the size of the result you display.

 s.reindex(index, method='ffill') data 2012-01-01 00:00:00.250000 12 2012-01-01 00:00:01.250000 45 2012-01-01 00:00:02.250000 56 2012-01-01 00:00:03.250000 89 2012-01-01 00:00:04.250000 12 
+5
source

Source: https://habr.com/ru/post/1469363/


All Articles