Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over months between two dates in Python? [duplicate]

How do I iterate over a timespan after days, hours, weeks or months?

Something like:

for date in foo(from_date, to_date, delta=HOURS):
    print date

Where foo is a function, returning an iterator. I've been looking at the calendar module, but that only works for one specific year or month, not between dates.

like image 259
sverrejoh Avatar asked Sep 30 '08 15:09

sverrejoh


People also ask

How do I get the difference in months between two dates in Python?

Use the relativedelta. months + relativedelta. years * 12 formula to get the total months between two dates.

How do I extract the months between two dates?

Use the DATEDIF function method if you want to get the total number of completed months in between two dates (it ignores the start date) Use the YEARFRAC method when you want to get the actual value of months elapsed between tow dates.

How do I get the difference between two dates in Python?

Use the strptime(date_str, format) function to convert a date string into a datetime object as per the corresponding format . To get the difference between two dates, subtract date2 from date1. A result is a timedelta object.


4 Answers

Use dateutil and its rrule implementation, like so:

from dateutil import rrule
from datetime import datetime, timedelta

now = datetime.now()
hundredDaysLater = now + timedelta(days=100)

for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, until=hundredDaysLater):
    print dt

Output is

2008-09-30 23:29:54
2008-10-30 23:29:54
2008-11-30 23:29:54
2008-12-30 23:29:54

Replace MONTHLY with any of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY. Replace dtstart and until with whatever datetime object you want.

This recipe has the advantage for working in all cases, including MONTHLY. Only caveat I could find is that if you pass a day number that doesn't exist for all months, it skips those months.

like image 144
Thomas Vander Stichele Avatar answered Jan 03 '23 23:01

Thomas Vander Stichele


I don't think there is a method in Python library, but you can easily create one yourself using datetime module:

from datetime import date, datetime, timedelta

def datespan(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate < endDate:
        yield currentDate
        currentDate += delta

Then you could use it like this:

>>> for day in datespan(date(2007, 3, 30), date(2007, 4, 3), 
>>>                     delta=timedelta(days=1)):
>>>     print day
2007-03-30
2007-03-31
2007-04-01
2007-04-02

Or, if you wish to make your delta smaller:

>>> for timestamp in datespan(datetime(2007, 3, 30, 15, 30), 
>>>                           datetime(2007, 3, 30, 18, 35), 
>>>                           delta=timedelta(hours=1)):
>>>     print timestamp
2007-03-30 15:30:00
2007-03-30 16:30:00
2007-03-30 17:30:00
2007-03-30 18:30:00
like image 28
DzinX Avatar answered Jan 04 '23 00:01

DzinX


For iterating over months you need a different recipe, since timedeltas can't express "one month".

from datetime import date

def jump_by_month(start_date, end_date, month_step=1):
    current_date = start_date
    while current_date < end_date:
        yield current_date
        carry, new_month = divmod(current_date.month - 1 + month_step, 12)
        new_month += 1
        current_date = current_date.replace(year=current_date.year + carry,
                                            month=new_month)

(NB: you have to subtract 1 from the month for the modulus operation then add it back to new_month, since months in datetime.dates start at 1.)

like image 20
giltay Avatar answered Jan 03 '23 23:01

giltay


I achieved this using pandas and datetime libraries as follows. It was much more convenient for me.

import pandas as pd
from datetime import datetime


DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

start_datetime = datetime.strptime('2018-05-18 00:00:00', DATE_TIME_FORMAT)
end_datetime = datetime.strptime('2018-05-23 13:00:00', DATE_TIME_FORMAT)

timedelta_index = pd.date_range(start=start_datetime, end=end_datetime, freq='H').to_series()
for index, value in timedelta_index.iteritems():
    dt = index.to_pydatetime()
    print(dt)
like image 26
Thilina Madumal Avatar answered Jan 03 '23 23:01

Thilina Madumal