Skip to content
Advertisement

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:

JavaScript

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

JavaScript

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 ?

Advertisement

Answer

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():

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement