0

I have a data frame that has a date column, what I need is to create another 2 columns with the "start of week date" and "end of week date". The reason for this is that I will then need to group by an "isoweek" column... but also keep this two-column "start_of_week_date" and "end_of_week_date"

I've created the below function:

def myfunc(dt, option):

wkday = dt.isoweekday()

if option == 'start':
    delta = datetime.timedelta(1 - wkday)
elif option == 'end':
    delta = datetime.timedelta(7 - wkday)
else:
    raise TypeError
    
return date + delta

Now I don't know how I would use the above function to populate the columns.

Probably don't even need my function to get what I need... which is... I have a DF that has the below columns

\>>> date, isoweek, qty

I will need to change it to:

\>>> isoweek, start_of_week_date, end_of_week_date, qty

this would then make my data go from 1.8 million rows to 300 thousand rows :D

can someone help me?

thank you

  • 1
    Hey! Does this help you? [Apply pandas function to column to create multiple new columns.](https://stackoverflow.com/questions/16236684/apply-pandas-function-to-column-to-create-multiple-new-columns) – mr bean Apr 02 '22 at 15:04

2 Answers2

0

Hope I understand correctly, Refer this dt.weekday for caculating week start & week end, here I've used 6 for 'Sunday' if you need any other day as weekend then give the appropriate number.

The day of the week with Monday=0, Sunday=6

df['start_of_week_date'] = df['Date'] - df['Date'].dt.weekday.astype('timedelta64[D]')
df['end_of_week_date'] = df['Date'] + (6 - df['Date'].dt.weekday).astype('timedelta64[D]') 
Vignesh
  • 231
  • 1
  • 6
0

There might be builtin functions that one can use and i can see one of the answers proposes such.

However, if you wish to apply your own function (which is perfectly acceptable) then could use the apply with lambda.

Here is an example:

import pandas as pd
from datetime import datetime

# an example dataframe
d = {'some date':[1,2,3,4],
    'other data':[2,4,6,8]}
df = pd.DataFrame(d)


# user defined function from the question
def myfunc(dt, option):

    wkday = dt.isoweekday()

    if option == 'start':
        delta = datetime.timedelta(1 - wkday)
    elif option == 'end':
        delta = datetime.timedelta(7 - wkday)
    else:
        raise TypeError
        
    return date + delta


df['new_col'] = df.apply(lambda x: myfunc(df['some data'], df['other data']), axis=1)
D.L
  • 4,339
  • 5
  • 22
  • 45