0

Let's say I have 11 Sessions for myself to complete. I haven't set dates for these sessions but rather just weekdays where one session would take place. Let's say when scheduling these sessions, I chose MON, TUE and WED. This means that after today, I want the dates to 11 my sessions which would be 4 Mondays, 4 Tuesdays and 3 Wednesdays from now after which my sessions will be completed.

I want to automatically get the dates for these days until there are 11 dates in total.

I really hope this makes sense... Please help me. I've been scratching my head over this for 3 hours straight.

Thanks,

Rocky Cipher
  • 99
  • 10
  • 1
    what code you have wriiten for solving this? – ParthS007 Apr 06 '20 at 14:15
  • So as a starting point, I figured that we can have two lists. One containing all the weekdays we need `days = [1,2,3]` and an empty `dates =[]` list. I know we have to get the current day and compare it with each day in the list to see of it's greater than today. If it is, we just ignore it and of it's not, this meanns that a new session should be made for the that weekday in this week. I can easily get that weekday by the subtracting todays day from the iterated weekday and adding that number to todays date to get the date required date. But what do I do for the next week is what I don't get – Rocky Cipher Apr 06 '20 at 14:22
  • Actually, let me show you some code in a few moments. I just got a new idea – Rocky Cipher Apr 06 '20 at 14:25

2 Answers2

1

You can use pd.date_range and the CustomBusinessDay object to do this very easily.

You can use the CustomBusinessDay to specify your "business days" and create your date range from it:

import pandas
from datetime import date

session_days = pd.offset.CustomBusinessDay(weekmask="Mon Tue Wed")
dates = pd.date_range(date.today(), freq=session_days, periods=11)
Daniel Geffen
  • 1,777
  • 1
  • 11
  • 16
  • I'm having problems with `_bz2` in Python3.7 . Pandas is installed correctly. But thank you for your contribution. I really appreciate it. – Rocky Cipher Apr 06 '20 at 15:46
0

I figured it out a while ago but my internet died. All it took was Dunhill and some rest.

import datetime


def get_dates():
    #This is the max number of dates you want. In my case, sessions.
    required_sessions = 11
    #These are the weekdays you want these sessions to be
    days = [1,2,3]
    #An empty list to store the dates you get
    dates = []
    #Initialize a variable for the while loop
    current_sessions = 0
    #I will start counting from today but you can choose any date
    now = datetime.datetime.now()
    #For my use case, I don't want a session on the same day I run this function.
    #I will start counting from the next day
    if now.weekday() in days:
        now = now + datetime.timedelta(days=1)

    while current_sessions != required_sessions:
        #Iterate over every day in your desired days
        for day in days:
            #Just a precautionary measure so the for loops breaks as soon as you have the max number of dates
            #Or the while loop will run for ever
            if current_sessions == required_sessions:
                break
            #If it's Saturday, you wanna hop onto the next week
            if now.weekday() == 6:
                #Check if Sunday is in the days, add it
                if 0 in days:
                    date = now + datetime.timedelta(days=1)
                    dates.append(date)
                    current_sessions += 1
                    now = date
            else:
                #Explains itself.
                if now.weekday() == day:
                    dates.append(now)
                    now = now + datetime.timedelta(days=1)
                    current_sessions += 1
                #If the weekday today is greater than the day you're iterating over, this means you've iterated over all the days in a NUMERIC ORDER
                #NOTE: This only works if the days in your "days" list are in a correct numeric order meaning 0 - 6. If it's random, you'll have trouble
                elif not now.weekday() > day:
                    difference = day - now.weekday()
                    date = now + datetime.timedelta(days=difference)
                    dates.append(date)
                    now = date
                    current_sessions += 1
        #Reset the cycle after the for loop is done so you can hop on to the next week.
        reset_cycle_days = 6 - now.weekday()
        if reset_cycle_days == 0:
            original_now = now + datetime.timedelta(days=1)
            now = original_now
        else:
            original_now = now + datetime.timedelta(days=reset_cycle_days)
            now = original_now
    for date in dates:(
        print(date.strftime("%d/%m/%y"), date.weekday()))

Btw, I know this answer is pointless compared to @Daniel Geffen 's answer. If I were you, I would definitely choose his answer as it is very simple. This was just my contribution to my own question in case anyone would want to jump into the "technicalities" of how it's done by just using datetime. For me, this works best as I'm having issues with _bz2 in Python3.7 .

Thank you all for your help.

Rocky Cipher
  • 99
  • 10