Skip to content

BUG: pandas to_datetime() returns incorrect isoweek conversion in week 53 when only 52 weeks exist #60885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
jdegene opened this issue Feb 8, 2025 · 1 comment · Fixed by #60896
Closed
3 tasks done
Labels
Bug datetime.date stdlib datetime.date support

Comments

@jdegene
Copy link

jdegene commented Feb 8, 2025

Pandas version checks

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

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

  • 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 : 0691c5c
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

@jdegene jdegene added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 8, 2025
@rhshadrach
Copy link
Member

rhshadrach commented Feb 8, 2025

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!

@rhshadrach rhshadrach added datetime.date stdlib datetime.date support and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug datetime.date stdlib datetime.date support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants