Home > Back-end >  Display of the correct year of the calendar week
Display of the correct year of the calendar week

Time:07-01

I am looking for the exact year of the calendar week:

data = {'Date': ['31.12.2022','01.01.2023','02.01.2023','31.12.2023','01.01.2024','02.01.2024','31.12.2024','01.01.2025','02.01.2025']}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'], format="%d.%m.%Y")
df['Week'] = df['Date'].dt.isocalendar().week
df['YearWeek'] = df['Date'].dt.strftime('%y%V')
df['Year'] = df['Date'].dt.year
df['Year_1'] = (df['Date'] pd.to_timedelta(6-df['Date'].dt.weekday, unit='d')).dt.year
df

result:

    Date     Week   YearWeek Year   Year_1
0   2022-12-31  52  2252    2022    2023
1   2023-01-01  52  2352    2023    2023
2   2023-01-02  1   2301    2023    2023
3   2023-12-31  52  2352    2023    2023
4   2024-01-01  1   2401    2024    2024
5   2024-01-02  1   2401    2024    2024
6   2024-12-31  1   2401    2024    2025
7   2025-01-01  1   2501    2025    2025
8   2025-01-02  1   2501    2025    2025

but I need YearWeek 2252 for 2023-01-01 (Row 1) or YearWeek 2501 for 2024-12-31 (Row 6). What did I wrong?

CodePudding user response:

IIUC you want ISO year week output (not the datetime's year), so something like this should work:

df['WeekYear'] = df['Date'].dt.isocalendar().year
df['Week'] = df['Date'].dt.isocalendar().week

df['YW'] = df['WeekYear'].astype(str).str[2:]   df['Week'].astype(str).str.zfill(2)

df[['Date', 'YW']]

        Date    YW
0 2022-12-31  2252
1 2023-01-01  2252
2 2023-01-02  2301
3 2023-12-31  2352
4 2024-01-01  2401
5 2024-01-02  2401
6 2024-12-31  2501
7 2025-01-01  2501
8 2025-01-02  2501

CodePudding user response:

The point is, ISO 8601 specifies that the week containing the first Thursday of the year is the first week of the year, and is numbered as week 1. This is also known as European week numbering.

So depending on the year, YYYY-01-01 may be included in week 1 of year YYYY, or in week 52 (or possibly 53!) of year YYYY-1. And YYYY-12-31 may be in week 1 of year YYYY 1.

  • Related