I have a data frame of dates:
id|date
13|2017-01-31
12|2016-12-07
11|2013-03-19
I want to classify the dates into weather it is the 1st, 2nd, 3rd or 4th week of the month.
I am using this function:
calendar.setfirstweekday(calendar.SUNDAY)
def get_week_of_month(dt):
year = dt.year
month = dt.month
day = dt.day
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x==day)[0][0] + 1
return(week_of_month)
df['week_month'] = df['date'].apply(get_week_of_month)
However I am getting 6 possible weeks instead of 4. Please help