Skip to content

Commit a34c9bc

Browse files
committed
handle week as fallback
1 parent 5fdad9a commit a34c9bc

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

pandas/_libs/tslibs/parsing.pyx

+14-15
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,6 @@ def parse_datetime_string(
289289
dt, _ = dateutil_parse(date_string, default=_DEFAULT_DATETIME,
290290
dayfirst=dayfirst, yearfirst=yearfirst,
291291
ignoretz=False)
292-
293-
if dt.tzinfo is not None:
294-
# dateutil can return a datetime with a tzoffset outside of (-24H, 24H)
295-
# bounds, which is invalid (can be constructed, but raises if we call
296-
# str(dt)). Check that and raise here if necessary.
297-
try:
298-
dt.utcoffset()
299-
except ValueError as err:
300-
# offset must be a timedelta strictly between -timedelta(hours=24)
301-
# and timedelta(hours=24)
302-
raise ValueError(
303-
f'Parsed string "{date_string}" gives an invalid tzoffset, '
304-
"which must be between -timedelta(hours=24) and timedelta(hours=24)"
305-
)
306-
307292
return dt
308293

309294

@@ -656,6 +641,20 @@ cdef dateutil_parse(
656641
ret = ret.replace(tzinfo=_dateutil_tzutc())
657642
elif res.tzoffset:
658643
ret = ret.replace(tzinfo=tzoffset(res.tzname, res.tzoffset))
644+
645+
# dateutil can return a datetime with a tzoffset outside of (-24H, 24H)
646+
# bounds, which is invalid (can be constructed, but raises if we call
647+
# str(ret)). Check that and raise here if necessary.
648+
try:
649+
ret.utcoffset()
650+
except ValueError as err:
651+
# offset must be a timedelta strictly between -timedelta(hours=24)
652+
# and timedelta(hours=24)
653+
raise ValueError(
654+
f'Parsed string "{timestr}" gives an invalid tzoffset, '
655+
"which must be between -timedelta(hours=24) and timedelta(hours=24)"
656+
)
657+
659658
return ret, reso
660659

661660

pandas/_libs/tslibs/period.pyx

+9-8
Original file line numberDiff line numberDiff line change
@@ -2593,16 +2593,17 @@ class Period(_Period):
25932593
value = value.upper()
25942594

25952595
freqstr = freq.rule_code if freq is not None else None
2596-
25972596
try:
25982597
dt, reso = parse_datetime_string_with_reso(value, freqstr)
2599-
except ValueError:
2598+
except ValueError as err:
26002599
match = re.search(r"^\d{4}-\d{2}-\d{2}/\d{4}-\d{2}-\d{2}", value)
2601-
if not match:
2602-
raise
2603-
# Case that cannot be parsed (correctly) by our datetime
2604-
# parsing logic
2605-
dt, freq = parse_weekly_str(value, freq)
2600+
if match:
2601+
# Case that cannot be parsed (correctly) by our datetime
2602+
# parsing logic
2603+
dt, freq = _parse_weekly_str(value, freq)
2604+
else:
2605+
raise err
2606+
26062607
else:
26072608
if reso == "nanosecond":
26082609
nanosecond = dt.nanosecond
@@ -2678,7 +2679,7 @@ def validate_end_alias(how: str) -> str: # Literal["E", "S"]
26782679
return how
26792680

26802681

2681-
cdef parse_weekly_str(value, BaseOffset freq):
2682+
cdef _parse_weekly_str(value, BaseOffset freq):
26822683
"""
26832684
Parse e.g. "2017-01-23/2017-01-29", which cannot be parsed by the general
26842685
datetime-parsing logic. This ensures that we can round-trip with

0 commit comments

Comments
 (0)