Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 25 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,37 @@ 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",
],
],
)
@pytest.mark.parametrize("errors", ["raise"])
def test_invalid_iso_week_53(self, msg, s, _format, errors):
# See GH#60885
with pytest.raises(ValueError, match=msg):
to_datetime(s, format=_format, errors=errors)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@pytest.mark.parametrize("errors", ["raise"])
def test_invalid_iso_week_53(self, msg, s, _format, errors):
# See GH#60885
with pytest.raises(ValueError, match=msg):
to_datetime(s, format=_format, errors=errors)
def test_invalid_iso_week_53(self, msg, s, _format):
# See GH#60885
with pytest.raises(ValueError, match=msg):
to_datetime(s, format=_format)

errors="raise" is the default so we don't need to specify it here


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