isocalendar()
assigns the week number 1 to the first calendar week of a year containing a Thursday:
https://docs.python.org/3/library/datetime.html#datetime.date.isocalendar
strftime('%U')
and strftime('%W')
assign the week number 1 to the first calendar week of a year containing a Sunday and Monday, respectively:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
If a return value of the Excel function WEEKNUM(..., return_type=1)
is the desired output, use strftime('%U').astype(int) + 1
:
https://support.microsoft.com/en-us/office/weeknum-function-e5c43a03-b4ab-426c-b411-b18c13c75340
=========
idx_dt = pd.date_range('2021-12-31', periods=7, freq='D')
df = pd.DataFrame(
{
'Weekday (str)': idx_dt.strftime('%Y-%m-%d (%a)'),
'Weeknum (isocalendar)': idx_dt.isocalendar().week,
'Weeknum (%U)': idx_dt.strftime('%U').astype(int),
'Weeknum (Excel)': [53, 1, 2, 2, 2, 2, 2],
},
index=idx_dt)
Weekday (str) Weeknum (isocalendar) Weeknum (%U) Weeknum (Excel)
2021-12-31 (Fri) 52 52 53
2022-01-01 (Sat) 52 0 1
2022-01-02 (Sun) 52 1 2
2022-01-03 (Mon) 1 1 2
2022-01-04 (Tue) 1 1 2
2022-01-05 (Wed) 1 1 2
2022-01-06 (Thu) 1 1 2