Home > Mobile >  Given a list of week numbers, how to organize the dates by weekly and split it by the end of month?
Given a list of week numbers, how to organize the dates by weekly and split it by the end of month?

Time:09-28

I have weekNumList = ['202234', '202235'] where 2022 represents the year and the last 2 digits, 34, represents the week number.

Week 202234 has the end of August and the beginning of September. I'd like to separate it to become like this: [['2022-08-28', '2022-08-29', '2022-08-30', '2022-08-31'], ['2022-09-01', '2022-09-02', '2022-09-03']]

So far, my code only separates it by week:

def returnWeeks(year, week):
  days = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 
          'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}
  a = datetime.strptime('{0}'.format(year), '%Y')   timedelta(days=7*(week-1))
  a  = timedelta(days=7-days.get(a.strftime('%A'), 0))
  for k in range(0, 7):
    yield (a   timedelta(days=k)).strftime('%Y-%m-%d')

days = list(returnWeeks(int(weekNumList[1][:4]), int(weekNumList[1][4:])))
print(days) # ['2022-08-28', '2022-08-29', '2022-08-30', '2022-08-31', '2022-09-01', '2022-09-02', '2022-09-03']

Any ideas how to implement splitting the dates if the week includes the end of month? Thank you!

CodePudding user response:

You could do groupby on your days list.

from datetime import datetime
from itertools import groupby
result = [
        list(l)
        for k, l in groupby(days, key=lambda x: datetime.strptime(x, "%Y-%m-%d").month)
    ]

Output

[['2022-08-28', '2022-08-29', '2022-08-30', '2022-08-31'],
 ['2022-09-01', '2022-09-02', '2022-09-03']]

CodePudding user response:

Pandas date_range makes the code a lot cleaner. You can convert the string to datetime using the format '%G%V%w' as long as you add a '0' to the date (indicating you want Sunday).

We make a date_range between the two dates, excluding the last day, then groupby the first 7 characters of each date YYYY-MM to split them into sub lists.

import pandas as pd
from datetime import datetime
from itertools import groupby

weekNumList = ['202234', '202235']

days = [list(dates) for _, dates in groupby(pd.date_range(*[pd.to_datetime(x '0',
                                                                           format='%G%V%w') for x in weekNumList],
                                                          closed='left').astype(str),
                                            key=lambda x: x[:7])]

Output

[['2022-08-28', '2022-08-29', '2022-08-30', '2022-08-31'],
 ['2022-09-01', '2022-09-02', '2022-09-03']]
  • Related