0

I have a range of dates

ie 2020/6/7 - 2020/6/10 and then do the following

  1. Add 20 calendar days
  2. Checks whether it is a weekend or a public holiday
  3. If it fall on a weekend or public holiday, add days until it falls on the next nearest business day

The date doesn't update after exiting the loop statement.

twentycalender_after = []
public_holiday = []

start_dt = datetime.date(2020, 6, 6)
end_dt = datetime.date(2020, 6, 10)
mydates = pd.date_range(start_dt, end_dt)

for da_te in mydates:
    twentycalender_after = da_te + datetime.timedelta(20)
    print(twentycalender_after)
    print(twentycalender_after.isoweekday())
    while twentycalender_after.isoweekday() >5:
        twentycalender_after += datetime.timedelta(1)
    print(twentycalender_after)                   ## HERE
    print(twentycalender_after.isoweekday())      ## HERE
    print('over')

Output is as follows:

2020-06-26 00:00:00
5
2020-06-26 00:00:00
5
over
2020-06-27 00:00:00
6
2020-06-29 00:00:00
1
over
2020-06-28 00:00:00
7
2020-06-29 00:00:00
1
over
2020-06-29 00:00:00
1
2020-06-29 00:00:00
1
over
2020-06-30 00:00:00
2
2020-06-30 00:00:00
2
over

However when I indent the lines marked HERE:

for da_te in mydates:
    twentycalender_after = da_te + datetime.timedelta(20)
    print(twentycalender_after)
    print(twentycalender_after.isoweekday())
    while twentycalender_after.isoweekday() >5:
        twentycalender_after += datetime.timedelta(1)
        print(twentycalender_after)                 ## HERE
        print(twentycalender_after.isoweekday())    ## HERE

    print('over')

The output(looks fine) is

2020-06-26 00:00:00
5
over
2020-06-27 00:00:00
6
2020-06-28 00:00:00
7
2020-06-29 00:00:00
1
over
2020-06-28 00:00:00
7
2020-06-29 00:00:00
1
over
2020-06-29 00:00:00
1
over
2020-06-30 00:00:00
2
over

Tom
  • 8,310
  • 2
  • 16
  • 36
fluervion
  • 29
  • 6
  • 1
    Everything looks fine here. Your first version just prints a before and after. The second version print a before and then 0 or more pairs of lines during the loop. – quamrana Jun 07 '20 at 14:29
  • `The date doesn't update after..` which date variable are you trying to change? – wwii Jun 07 '20 at 14:33
  • @quamrana Thanks quamrana, realised what's going on – fluervion Jun 10 '20 at 14:13

1 Answers1

0

Your change to the indentation of the print statements is only changing what the console prints out, not any of the data. The first version only prints the date (and if its a weekday) before and after its been changed, while the second prints the date before, and then how it changes in each iteration of the while loop. I believe the first version is actually doing the processing you want, but you may need to be more specific about what you are looking for as output.

One thing though is that the new dates are not being stored anywhere. You start with a list twentycalender_after = [], but then in your for loop you reassign that name to reference the changed date: twentycalender_after = da_te + datetime.timedelta(20). You should append the new dates created in the loop to the list you start with:

output = []  #picking a new name to avoid confusion

start_dt = datetime.date(2020, 6, 6)
end_dt = datetime.date(2020, 6, 10)
mydates = pd.date_range(start_dt, end_dt)

for da_te in mydates:
    twentycalender_after = da_te + datetime.timedelta(20)
    print(twentycalender_after)
    print(twentycalender_after.isoweekday())
    while twentycalender_after.isoweekday() >5:
        twentycalender_after += datetime.timedelta(1)
    print(twentycalender_after)                   ## HERE
    print(twentycalender_after.isoweekday())      ## HERE
    output.append(twentycalender_after)
    print('over')

Output:

[Timestamp('2020-06-26 00:00:00', freq='D'),
 Timestamp('2020-06-29 00:00:00', freq='D'),
 Timestamp('2020-06-29 00:00:00', freq='D'),
 Timestamp('2020-06-29 00:00:00', freq='D'),
 Timestamp('2020-06-30 00:00:00', freq='D')]

This doesn't address the public holiday bit yet though, but you could use something like this to check if each date is on a public holiday (as an or condition in the while loop).

Tom
  • 8,310
  • 2
  • 16
  • 36
  • Thank you for helping to review my post and for your answer, Tom! You are right, I have changed the twentycalender_after to another variable name and have appended the output to the list. – fluervion Jun 10 '20 at 14:12