Search code examples
pythonpandasscipyresamplingdownsampling

How to resample / downsample an irregular timestamp list?


SImple question but I haven't been able to find a simple answer.

I have a list of data which counts the time in seconds that events occur:

[200.0 420.0 560.0 1100.0 1900.0 2700.0 3400.0 3900.0 4234.2 4800.0 etc..]

I want to count how many events occur each hour (3600 seconds) and create a new list of these counts.

I understand this is called downsampling, but all the information I can find is related to traditional time series.

For the example above the new list would look like:

[7 3 etc..]

Any help would be greatly appreciated.


Solution

  • all_events = [
        200.0, 420.0, 560.0, 1100.0, 1900.0, 2700.0, 3400.0, 3900.0, 4234.2, 4800.0]
    
    def get_events_by_hour(all_events):
        return [
            len([x for x in all_events if int(x/3600.0) == hour]) 
            for hour in xrange(24)
        ]
    
    print get_events_by_hour(all_events)
    

    Note that all_events should contain events for one day.