Home > Blockchain >  How to reduce if statements Python?
How to reduce if statements Python?

Time:06-07

I have a df of holiday dates and I'm trying to make a list of dates six weeks before the holiday to add to the df. There are three lists PreList (the day before), DuringList (the day off) and PostList (the day after). I am using datetime.timedelta to get the six weeks before the holiday but I don't like all of the if statements. How can I reduce the if statements?

PreList = []
DuringList = []
PostList = []

for i in range(len(df)):
    
    #for Mon
    if df.date[i].weekday() == 0:
        preDay = df.date[i] - datetime.timedelta(days = 3)
        preWeek = preDay - datetime.timedelta(weeks = 6)
        during = df.date[i]
        postDay = df.date[i]   datetime.timedelta(days = 1)
        postWeek = postDay - datetime.timedelta(weeks = 6)
        PreList.append(preWeek)
        DuringList.append(during)
        PostList.append(postWeek)
        
    #for Fri
    elif df.date[i].weekday() == 4:
        preDay = df.date[i] - datetime.timedelta(days = 1)
        preWeek = preDay - datetime.timedelta(weeks = 6)
        during = df.date[i]
        postDay = df.date[i]   datetime.timedelta(days = 3)
        postWeek = postDay - datetime.timedelta(weeks = 6)
        PreList.append(preWeek)
        DuringList.append(during)
        PostList.append(postWeek)
        
    #for Sat
    elif df.date[i].weekday() == 5:
        pre = df.date[i] - datetime.timedelta(days = 1)
        preWeek = preDay - datetime.timedelta(weeks = 6)
        during = df.date[i]
        postDay = df.date[i]   datetime.timedelta(days = 2)
        postWeek = postDay - datetime.timedelta(weeks = 6)
        PreList.append(preWeek)
        DuringList.append(during)
        PostList.append(postWeek)
        
    #for Sun
    elif df.date[i].weekday() == 6:
        preDay = df.date[i] - datetime.timedelta(days = 2)
        preWeek = preDay - datetime.timedelta(weeks = 6)
        during = df.date[i]
        postDay = df.date[i]   datetime.timedelta(days = 1)
        postWeek = postDay   datetime.timedelta(weeks = 6)
        PreList.append(preWeek)
        DuringList.append(during)
        PostList.append(postWeek)
        
    #for Tue - Thu
    else:
        preDay = df.date[i] - datetime.timedelta(days = 1)
        preWeek = preDay - datetime.timedelta(weeks = 6)
        during = df.date[i]
        postDay = df.date[i]   datetime.timedelta(days = 1)
        postWeek = postDay   datetime.timedelta(weeks = 6)
        PreList.append(preWeek)
        DuringList.append(during)
        PostList.append(postWeek)

CodePudding user response:

You can use a dict to save the parameters.


PreList = []
DuringList = []
PostList = []

params_map = {0: [3, 6, 1, 6],
              1: [1, 6, 1, 6],
              2: [1, 6, 1, 6],
              3: [1, 6, 1, 6],
              4: [1, 6, 3, 6],
              5: [1, 6, 2, 6],
              6: [2, 6, 1, 6]}

for i in range(len(df)):
    key = df.date[i].weekday()
    params = params_map[key]

    preDay = df.date[i] - datetime.timedelta(days=params[0])
    preWeek = preDay - datetime.timedelta(weeks=params[1])
    during = df.date[i]
    postDay = df.date[i]   datetime.timedelta(days=params[2])
    postWeek = postDay - datetime.timedelta(weeks=params[3])
    PreList.append(preWeek)
    DuringList.append(during)
    PostList.append(postWeek)

CodePudding user response:

I would personally use one line if statements, that then call a function with the code. This way it is cleaner, and the if statements use up less code.

weekday5() if df.date[i].weekday() == 5 

These each take up one line of code, and call a function. This is the simplest way that I know how to do it.

CodePudding user response:

I'd create a dict (or simpler yet a list, since the indices go from 0-6) of the timedeltas so you can generate the "pre/post" entries via a simple lookup:

PreList = []
DuringList = []
PostList = []

pre_post = [
    # Pre/post day offsets by weekday.
    (3, 1),      # Mon
    (43, 43),    # Tue
    (43, 43),    # Wed
    (43, 43),    # Thu
    (1, 3),      # Fri
    (1, 2),      # Sat
    (44, 43),    # Sun
]

for i in range(len(df)):
    d = df.date[i]
    pre, post = pre_post[d.weekday()]
    PreList.append(d - datetime.timedelta(days=pre))
    DuringList.append(d)
    PostList.append(d   datetime.timedelta(days=post))

Not only does this cut down on the number of if statements and the amount of overall code, it makes it easy to see at a glance what the offsets are for each day in one place (and update if needed) instead of having to scroll through a bunch of other code.

  • Related