0

I am looking to find the current week of the month. There are many answers already on this here but i have below scenario:

  1. Week starts from Sunday-Saturday

  2. When month changes, majority of the dates should be considered. Example, 30th March 2020 is Week1 of April since in that week, there are 3 dates of March(29, 30, 31) but 4 dates of April(1,2,3,4).

  3. Sample start dates and end dates are shown below:

        start date  end date
Jan 1   12/29/2019  2/1/2020
Feb 2   2/2/2020    2/29/2020
Mar 3   3/1/2020    3/28/2020
Apr 4   3/29/2020   5/2/2020
May 5   5/3/2020    5/30/2020
Jun 6   5/31/2020   6/27/2020
Jul 7   6/28/2020   8/1/2020
Aug 8   8/2/2020    8/29/2020
Sep 9   8/30/2020   9/26/2020
Oct 10  9/27/2020   10/31/2020
Nov 11  11/1/2020   11/28/2020
Dec 12  11/29/2020  12/26/2020

I am doing it via pd.merge where i have created complete table for 1 year which i can lookup and find the date but i am looking for something automated which will not be required to be updated every year.

RSM
  • 645
  • 10
  • 25
  • Sorry, it's really unclear what you're asking. This seems more like an analytical design question, rather than a programming question. If so, maybe try the stack exchange network sites [code review](https://codereview.stackexchange.com/) or [software engineering](https://softwareengineering.stackexchange.com/). In either case, you'll need to post the code you've tried - there aren't any "please write my code for me" sites. Hope this helps! – Michael Delgado May 14 '20 at 01:37
  • @MichaelDelgado: please see my approach in the answer below. This is a manual way. I am looking for something more precise which would not involve a manual updating of ```df_week``` table every year. – RSM May 14 '20 at 03:26

1 Answers1

0

I have created a table for the complete year as follows: (I have truncated the dataframe below to save space but it continues till complete year end)

df_week

    Month   Week    1   2   3   4   5   6   7
0   Jan W1  2019-12-29  2019-12-30  2019-12-31  2020-01-01  2020-01-02  2020-01-03  2020-01-04
1   Jan W2  2020-01-05  2020-01-06  2020-01-07  2020-01-08  2020-01-09  2020-01-10  2020-01-11
2   Jan W3  2020-01-12  2020-01-13  2020-01-14  2020-01-15  2020-01-16  2020-01-17  2020-01-18
3   Jan W4  2020-01-19  2020-01-20  2020-01-21  2020-01-22  2020-01-23  2020-01-24  2020-01-25
4   Jan W5  2020-01-26  2020-01-27  2020-01-28  2020-01-29  2020-01-30  2020-01-31  2020-02-01
5   Feb W1  2020-02-02  2020-02-03  2020-02-04  2020-02-05  2020-02-06  2020-02-07  2020-02-08
6   Feb W2  2020-02-09  2020-02-10  2020-02-11  2020-02-12  2020-02-13  2020-02-14  2020-02-15

Then i used the following:

# Finds today's date in YYYY-MM-DD format
>d = dt.datetime.today().strftime("%Y-%m-%d") 

#Find matching row in complete dataframe corresponding to the current date
>cal_week = df_week[df_week.eq(d).any(1)]["Week"].iat[0] 
>cal_week 
W2
RSM
  • 645
  • 10
  • 25