TransWikia.com

Getting all starting dates of year 2020 with ISO week frequency

Stack Overflow Asked by umbe1987 on November 1, 2020

I’d like to create a list of dates each of which represents the starting date of ISO week N of year 2020.

Something like:

weeks2020 = [date(2020, 1, 1), date(2020, 1, 6), date(2020, 1, 13), ...]

I have obtained something similar using timedelta(weeks=1), and adding this to my START_DATE (date(2020, 1, 1)), but the dates I obtain are not correct.

I know I could simply change my START_DATE to be date(2019, 12, 30), but I would like to know if there is a more robust approach to derive all the week starting dates present in a given year.

Just for the sake of clarity, here is what i am doing now:

from datetime import date, timedelta

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(weeks=1)
STEPS = 54

prev_date = START_DATE

for i in range(1, STEPS):
    print(prev_date.strftime('%Y-%m-%d')) # step 1: 2020-01-01, step 2: 2020-01-08, ...
    prev_date += INTERVAL

3 Answers

For the first interval, find the weekday of the starting date and subtract this from a full week. After the first step, set the interval back to one week.

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(weeks=1) - timedelta(days=START_DATE.weekday())

cur_date = START_DATE

while cur_date.year == START_DATE.year:
    print(cur_date.strftime("%Y-%m-%d"))
    cur_date += INTERVAL
    INTERVAL = timedelta(weeks=1)

Correct answer by Wups on November 1, 2020

If you can use pandas, maybe something like this should be feasible-

import pandas as pd

di = pd.to_datetime(pd.date_range(start='2020-1-1', end='2020-12-31', freq='D'))
# check for either monday or start of the month
weekstart = di[(di.weekday == 0) | ((di.day == 1) & (di.weekday != 6)]
Output
DatetimeIndex(['2020-01-01', '2020-01-06', '2020-01-13', '2020-01-20',
               '2020-01-27', '2020-02-01', '2020-02-03', '2020-02-10',
               '2020-02-17', '2020-02-24', '2020-03-02', '2020-03-09',
               '2020-03-16', '2020-03-23', '2020-03-30', '2020-04-01',
               '2020-04-06', '2020-04-13', '2020-04-20', '2020-04-27',
               '2020-05-01', '2020-05-04', '2020-05-11', '2020-05-18',
               '2020-05-25', '2020-06-01', '2020-06-08', '2020-06-15',
               '2020-06-22', '2020-06-29', '2020-07-01', '2020-07-06',
               '2020-07-13', '2020-07-20', '2020-07-27', '2020-08-01',
               '2020-08-03', '2020-08-10', '2020-08-17', '2020-08-24',
               '2020-08-31', '2020-09-01', '2020-09-07', '2020-09-14',
               '2020-09-21', '2020-09-28', '2020-10-01', '2020-10-05',
               '2020-10-12', '2020-10-19', '2020-10-26', '2020-11-02',
               '2020-11-09', '2020-11-16', '2020-11-23', '2020-11-30',
               '2020-12-01', '2020-12-07', '2020-12-14', '2020-12-21',
               '2020-12-28'],
              dtype='datetime64[ns]', freq=None)

Answered by sai on November 1, 2020

Trying to answer myself.

I don't know if this is the best solution, but it seems to work fine for my aim:

from datetime import date, timedelta

START_DATE = date(2020, 1, 1)
INTERVAL = timedelta(days=1)
STEPS = 366

iso_week = 0

weeks_2020 = []

curr_date = START_DATE

for d in range (STEPS):
    if not curr_date.isocalendar()[1] == iso_week:
        iso_week = curr_date.isocalendar()[1]
        weeks_2020.append(curr_date)
    curr_date += INTERVAL

Answered by umbe1987 on November 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP