-1

I am in a process of making a fbprophet sales forecasting model. I am going to pass a list of holidays to model, and I was using a for loop to get sundays and the national holiday dates between 2 given years, and then append these values to an empty list 'ee'.

I am able to get all the sundays in the list but not the national holidays, there might be something wrong with the 2nd for loop. Can somebody help me? I'm trying to get a list of Sundays and dates - 26th Jan, 15th Aug, 2nd Oct between 2 years

Here is the code:

from datetime import date, timedelta

set =[]
def allsundays(year1,year2):
   d = date(year1, 1, 1)                    # January 1st
   d += timedelta(days = 6 - d.weekday())  # First Sunday
   while d.year <= year2:
      yield d
      d += timedelta(days = 7)


def nat_hol(year1,year2):
    j=0
    r= date(year1 +j,1,26)
    i= date(year1 +j,8,15)
    o= date(year1 +j,10,2)
    while (r.year <= year2 and i.year <= year2 and o.year <= year2):
        yield (r,i,o)
        j=j+1

for ee in nat_hol(2016,2021):
    set.append(ee.strftime("%Y-%m-%d"))
set

This is the output:

['2016-01-03',
 '2016-01-10',
 '2016-01-17',
 '2016-01-24',
 '2016-01-31',
 '2016-02-07',
 '2016-02-14',
 '2016-02-21',
 '2016-02-28',
 '2016-03-06',
 '2016-03-13',
 '2016-03-20',
 '2016-03-27',
 '2016-04-03',
 '2016-04-10',

1 Answers1

2

Regardless of the other problems with the posted example, your blocking problem is the infinite loop in your generator:

def nat_hol(year1,year2):
    j=0
    r= date(year1 +j,1,26)
    i= date(year1 +j,8,15)
    o= date(year1 +j,10,2)
    while (r.year <= year2 and i.year <= year2 and o.year <= year2):
        yield (r,i,o)
        j=j+1

You set the year of each date to year1 and never change it; if year1 <= year2, you'll return the same three dates forever.

When you increase j, you also must change the three dates.

This manipulation will become a lot easier if you use the datetime package and simply add an interval of 1 year on each call.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    I am new to computer languages.BTW i have updated the post to include the code i was using to get sundays. I got that from one of the posts of stackoverflow. Can you help me out with making that code work for those days too. – Ankit Gupta Jan 04 '20 at 07:48