1

I have below code:

from datetime import date
from datetime import timedelta
today = datetime.date.today() 

for i in range(0,7):
    print (today - timedelta(days=i))
2018-10-31
2018-10-30
2018-10-29
2018-10-28
2018-10-27
2018-10-26
2018-10-25

Want I want is just to print weekdays and excluding weekends. So, my desired result should be:

2018-10-31
2018-10-30
2018-10-29
2018-10-26
2018-10-25
2018-10-24
2018-10-23

Where can I modify my code to achieve aimed results?

MGB.py
  • 461
  • 2
  • 9
  • 25

1 Answers1

4

Use datetime.date.weekday(), which:

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

from datetime import date
from datetime import timedelta

today = date.today()

for i in range(7):
    d = today - timedelta(days=i)
    if d.weekday() < 5:            # Here
        print(d)

Produces:

2018-10-31
2018-10-30
2018-10-29
2018-10-26
2018-10-25

This gives you the weekdays that fall in the last 7 days. Or, if you want the previous 7 weekdays, consider:

from datetime import date
from datetime import timedelta

today = date.today()

num_weekdays = 0
for i in range(10):
    d = today - timedelta(days=i)
    if d.weekday() < 5:
        print(d)
        num_weekdays += 1

    if num_weekdays >= 7:
        break

This version is basically the same, with the range stop changed from 7 to 10, and an added num_weekdays counter. We increment the counter when we print a date, and once we hit 7, we break the loop (otherwise we may print 8 dates, depending on the day of the week of today).

Or, another way:

from datetime import date
from datetime import timedelta

today = date.today()

prev_days = [today - timedelta(days=i) for i in range(10)]  # Get 10 previous days
prev_days = [d for d in prev_days if d.weekday() < 5]       # Filter out the weekends
for d in prev_days[:7]:                                     # Select the first 7
    print(d)

Similar idea, we create a list of 10 previous dates called prev_days. We then filter that list down by filtering out weekend dates. Then, in the for loop, we only loop over the first 7 elements of the filtered list, so that we print at most 7 dates.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • you are almost there but your output just lists 5 dates but what if I want to list 7 weekdays? Thanks – MGB.py Oct 31 '18 at 20:07
  • 1
    @MGB.py I edited something to that effect in. It works but I'm not thrilled with it. If you don't need it immediately, consider checking back in 10 minutes or so to see if I've edited the answer again to something cleaner. – jedwards Oct 31 '18 at 20:08
  • Thanks that was what I was looking for. – MGB.py Oct 31 '18 at 20:09
  • I would be thankful if you could explain some lines what they are doing. I am little confuse to understand the code. Thanks – MGB.py Oct 31 '18 at 20:13
  • 1
    @MGB.py I added another version and some explanation about each – jedwards Oct 31 '18 at 20:21
  • Thank you very much. Now your code is very clean and easy to understand and to modify the number of weekdays. – MGB.py Oct 31 '18 at 20:28
  • How to apply your code to already existing date column as below: df['date'].value_counts().head(). I want to apply your code to date column. – MGB.py Nov 05 '18 at 09:25