Pandas version checks

  • [x] I have checked that this issue has not already been reported.

  • [x] I have confirmed this bug exists on the latest version of pandas.

  • [x] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
pd.to_datetime("2024-53-1", format="%G-%V-%u")

Issue Description

When using python format codes to resolve an isoweek notation ("%G-%V-%u") back to an iso date, to_datetime() incorrectly works with non-existing weeks. In the example below the third call should not return a valid date, as no week 53 exists in the isoclander year 2024 (the first lines are non-isoclandar, the last line is the first week in isocalendar 2025, which are all correct).

Image

This behavior can be seen in others years with 52 isoweeks as well, e.g. pd.to_datetime("2023-53-1", format="%G-%V-%u") also returns the date of the first isoweek of 2024.

The python standard library correctly raises an error when the same thing is tried:

Image

Expected Behavior

Raise an error for misformatted date string.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0691c5cf90477d3503834d983f69350f250a6ff7 python : 3.12.6 python-bits : 64 OS : Windows OS-release : 11 Version : 10.0.26100 machine : AMD64 processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD byteorder : little LC_ALL : None LANG : en LOCALE : English_Germany.1252 pandas : 2.2.3 numpy : 2.1.2 pytz : 2024.1 dateutil : 2.9.0 pip : 24.2 Cython : None sphinx : 8.0.2 IPython : 8.28.0 adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.3 blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : 1.1 hypothesis : None gcsfs : None jinja2 : 3.1.4 lxml.etree : None matplotlib : 3.9.2 numba : None numexpr : None odfpy : None openpyxl : 3.1.5 pandas_gbq : None psycopg2 : None pymysql : None pyarrow : 18.1.0 pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.14.1 sqlalchemy : None tables : None tabulate : 0.9.0 xarray : None xlrd : None xlsxwriter : None zstandard : 0.23.0 tzdata : 2024.2 qtpy : 2.4.1 pyqt5 : None

Comment From: rhshadrach

Thanks for the report! Agreed this should raise, it looks like we can add the code

if iso_week == 53:
    now = date.fromordinal(date(iso_year, 1, 1).toordinal() + ordinal - iso_weekday)
    jan_4th = date(iso_year+1, 1, 4)
    if (jan_4th - now).days < 7:
        raise ValueError("...")

to _libs.tslibs.strptime._calc_julian_from_V. But this is out of my element, need to make sure there isn't an off-by-1 error. Perhaps there is a better place to perform this check as well. Further investigations and PRs to fix are welcome!