3

This is rather confusing to ask because I understand there are date ranges that create dates between two dates (which isn't what I want).

Instead, what I'm looking for is literally a start to an end. I plan on making a data frame with the index being in the format of YYYY-MM-DD - YYYY-MM-DD.

So, the first row would have an index of 2000-01-01 - 2001-01-01, the second row would have an index of 2000-01-02 - 2001-01-02, the third row would have an index of 2000-01-03 - 2001-01-03, etc. Is there any way to do do this (or something similar)? I was considering making a class that paired two datetime.date objects together, but I'm hesitant as I'm sure there's a more fleshed out manner of doing this.

Nick Juelich
  • 428
  • 2
  • 13
commonelk
  • 63
  • 4
  • Does this answer your question? [Print all day-dates between two dates](https://stackoverflow.com/questions/7274267/print-all-day-dates-between-two-dates) – Aditya Jun 25 '20 at 17:06
  • @Aditya I could, for example, create a list that contains the start date and the time delta. However, at that point, I might as well just create a list that contains both the start date and the end date. Any other way? Also, as for the page you linked, that's what a date range should do. – commonelk Jun 25 '20 at 17:07
  • There is `pd.Period`. https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timestamps-vs-time-spans – Scott Boston Jun 25 '20 at 17:26

1 Answers1

2

Apologies if I didn't understand your question properly,

from datetime import datetime, timedelta 

def datetime_range(dt_1=None, dt_2=None, span=None): 
    for i in range(span + 1): 
        yield dt_1 + timedelta(days=i), dt_2 + timedelta(days=i)

dt_1, dt_2 = datetime(2014, 1, 1), datetime(2015, 1, 1)

for date_1,date_2 in datetime_range(dt_1, dt_2, 10):
    print(date_1, date_2) #.date() if needed..

# o/p's
2014-01-01 00:00:00 2015-01-01 00:00:00
2014-01-02 00:00:00 2015-01-02 00:00:00
2014-01-03 00:00:00 2015-01-03 00:00:00
2014-01-04 00:00:00 2015-01-04 00:00:00
2014-01-05 00:00:00 2015-01-05 00:00:00
2014-01-06 00:00:00 2015-01-06 00:00:00
...
Aditya
  • 2,380
  • 2
  • 14
  • 39
  • In the meantime, I created a series that contained lists with two dates, and then I set that series as the index. Nevertheless, what you wrote is essentially what I asked for, so thank you very much! – commonelk Jun 25 '20 at 18:40
  • I am glad it helped! – Aditya Jun 26 '20 at 18:35