Skip to content
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

fix: incorrect ISO week 53 conversion when only 52 weeks exist #60896

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,13 @@ cdef (int, int) _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday)

correction = date(iso_year, 1, 4).isoweekday() + 3
ordinal = (iso_week * 7) + iso_weekday - correction

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(f"Week 53 does not exist in ISO year {iso_year}.")

# ordinal may be negative or 0 now, which means the date is in the previous
# calendar year
if ordinal < 1:
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,36 @@ def test_to_datetime_np_str(self):
["2015-1-1", "%G-%V-%u", datetime(2014, 12, 29, 0, 0)],
["2015-1-4", "%G-%V-%u", datetime(2015, 1, 1, 0, 0)],
["2015-1-7", "%G-%V-%u", datetime(2015, 1, 4, 0, 0)],
["2024-52-1", "%G-%V-%u", datetime(2024, 12, 23, 0, 0)],
["2024-52-7", "%G-%V-%u", datetime(2024, 12, 29, 0, 0)],
["2025-1-1", "%G-%V-%u", datetime(2024, 12, 30, 0, 0)],
["2020-53-1", "%G-%V-%u", datetime(2020, 12, 28, 0, 0)],
],
)
def test_to_datetime_iso_week_year_format(self, s, _format, dt):
# See GH#16607
assert to_datetime(s, format=_format) == dt

@pytest.mark.parametrize(
"msg, s, _format",
[
[
"Week 53 does not exist in ISO year 2024",
"2024 53 1",
"%G %V %u",
],
[
"Week 53 does not exist in ISO year 2023",
"2023 53 1",
"%G %V %u",
],
],
)
def test_invalid_iso_week_53(self, msg, s, _format):
# See GH#60885
with pytest.raises(ValueError, match=msg):
to_datetime(s, format=_format)

@pytest.mark.parametrize(
"msg, s, _format",
[
Expand Down
Loading