11
votes

So I have a script that has date arguments for different functions and I want it to loop through 01-01-2012 to 06-09-2012 not including weekends. Im trying to figure out a way I can use time delta because my script outputs files with the date used in the name of the file for example:

items = (functions.getItems(item,date)
    print items
    test = sum(abs(l[-1]) for l in items)
    total = open('total' +str(datetime.today- datetime.timedelta(1)),'a')

I want timedelta(1) to cycle through each date so that the output file would have the format of total2012-01-01 for the first day and cycle through until it created the file total2012-06-09. Also the date argument for items has the format of MM-DD-YYYY

I thought that I could do this:

sd = 01-01-2012
ed = 06-09-2012
delta = datetime.timedelta(days=1)
diff = 0
while sd != ed
    # do functions 
    # (have output files (datetime.today - datetime.delta(diff))
    diff +=1
    sd+=delta

So essentially I'm just trying to figure out how can I loop through having the function start with 01-01-2012 and ending with 06-10-2012 excluding weekends. I'm having trouble figuring out how to exclude weekends and how to get it to loop in the proper order

Thanks

3
Please avoid using dates in the format MM-DD-YYYY. It is very confusing since the elements are neither in increasing nor in decreasing order of magnitude. Furthermore, it to easy to mix it up with DD-MM-YYYY, which is the second most reasonable date format after the ISO-standard YYYY-MM-DD. - Celada
I agree with @Celada, use the ISO format if possible. If that isn't possible, please use / instead of - as a separator to reduce the confusion. - Mark Ransom

3 Answers

17
votes

Use the datetime.weekday() method. It returns values between zero and six, related to the weekdays. Saturday value is 5 and Sunday value is 6; so, if you skip the operation when these values appear, you skip weekdends:

start = datetime(2012, 1, 1)
end = datetime(2012, 10, 6)
delta = timedelta(days=1)
d = start
diff = 0
weekend = set([5, 6])
while d <= end:
    if d.weekday() not in weekend:
        diff += 1
    d += delta
2
votes

There is easier way to do this using freq = 'B' for business day frequency.

import pandas as pd
dt = pd.date_range(start=datetime.date.today(), periods=10, freq='B')
dt

which gives you :

DatetimeIndex(['2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16',
               '2018-08-17', '2018-08-20', '2018-08-21', '2018-08-22',
               '2018-08-23', '2018-08-24'],
              dtype='datetime64[ns]', freq='B')

you check name of day also by:

dt.weekday_name

Index(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Monday',
       'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
      dtype='object')
1
votes

@brandizzi's answer is more syntactically aesthetic but as an alternative you can use the following

start = datetime(2012, 1, 1)
end = datetime(2012, 10, 6)
delta = timedelta(days=1)
d = start
diff = 0
SATURDAY = 5
while d <= end:
    if d.weekday() < SATURDAY:
        diff += 1
        d += delta