Thomas Johnson
Thomas Johnson

Reputation: 11688

Downsampling irregular time series in pandas

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 downsample it to 1 second data without aligning to 1-second boundaries? For instance, is there a way to get this data out (assuming downsampling a way where the latest value that occurs before or on the sample time is used):


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

Upvotes: 4

Views: 2754

Answers (1)

Dan Allan
Dan Allan

Reputation: 35255

Create a DateTimeIndex a frequency of 1 second and an offset of a quarter-second like so.

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

Conform your data to this index, and "fill forward" to downsample the way you show in your desired result.

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

Upvotes: 5

Related Questions