Search code examples
pythonpandasdatetimetimestampdate-range

Creating date range pairs in pandas


I have two datetimes between which I would like to generate regular intervals of 4 hours (excluding the last interval, which can be less than 4 hours if there are less than 4 hours between the previous timestamp and end).

I am stuck on interval generation with pandas.date_range, which only returns the end timestamp. For example:

import pandas
from datetime import datetime

start = datetime(2021, 4, 2, 20, 40, 0)
end = datetime(2021, 4, 4, 18, 20, 0)


dates = pandas.date_range(start=end, end=end, freq='4H')
dates

DatetimeIndex(['2021-04-04 18:20:00'], dtype='datetime64[ns]', freq='4H')

The aim is to generate a list of datetime pairs such as:

[['2021-04-02 20:40:00', '2021-04-03 00:40:00'], ['2021-04-03 00:40:00', '2021-04-03 04:40:00']...['2021-04-04 16:40:00', '2021-04-04 18:20:00']] #last pair can be cut off by `end`.  

What am I doing wrong in the syntax and how would one go about generating a paired list of intervals from using pandas.date_range ?


Solution

  • There was a typo (start=end) that caused dates to have only 1 value.

    But fixing the typo only gives you a flat range of dates. If you want those nested pairs, you could shift dates by 4 hours and zip():

    dates = pandas.date_range(start=start, end=end, freq='4H')
    shift = dates + pandas.Timedelta(hours=4)
    
    pairs = list(zip(dates, shift))
    
    # [(Timestamp('2021-04-02 20:40:00', freq='4H'),
    #   Timestamp('2021-04-03 00:40:00', freq='4H')),
    #  (Timestamp('2021-04-03 00:40:00', freq='4H'),
    #   Timestamp('2021-04-03 04:40:00', freq='4H')),
    #  (Timestamp('2021-04-03 04:40:00', freq='4H'),
    #   Timestamp('2021-04-03 08:40:00', freq='4H')),
    # ...
    

    Or for a list of lists instead of list of tuples:

    pairs = list(map(list, zip(dates, shift)))