Skip to content

Commit 99c5029

Browse files
authored
Fixes #3: Update parsing.pyx to handle multi-week period format.
1 parent ee2bc23 commit 99c5029

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

pandas/_libs/tslibs/parsing.pyx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -758,23 +758,8 @@ cdef _find_subsecond_reso(str timestr, int64_t* nanos):
758758
return reso
759759

760760

761-
# Parsing for iso_ordinal date
761+
# Parsing for iso_ordinal date, multi-year period, quarter-based multi-year period, and multi-week period
762762
# ----------------------------------------------------------------------
763-
def _parse_iso_ordinal_date(value: str):
764-
"""
765-
Parses an ISO 8601 ordinal date format (YYYY-DDD).
766-
767-
Example:
768-
"1981-095" → "1981-04-05"
769-
"""
770-
match = re.match(r"^(\d{4})-(\d{3})$", value)
771-
if match:
772-
year, day_of_year = match.groups()
773-
date = datetime.strptime(f"{year}-{day_of_year}", "%Y-%j").date()
774-
return f"{date.year}-{date.month:02d}-{date.day:02d}" # Convert to YYYY-MM-DD
775-
return None # Not a match
776-
777-
778763
def parse_time_string(time_str, freq=None):
779764
"""
780765
Extended parsing logic to handle:
@@ -792,6 +777,10 @@ def parse_time_string(time_str, freq=None):
792777
# Handle Multi-Quarter Spans (e.g., "2019Q1-2021Q4")
793778
multi_quarter_match = re.match(r"^(\d{4}Q[1-4])-(\d{4}Q[1-4])$", time_str)
794779

780+
# Handle Week Start-End Format (YYYYMMDD-YYYYMMDD)
781+
week_match = re.match(r"^(\d{8})-(\d{8})$", time_str)
782+
783+
795784
if ordinal_match:
796785
try:
797786
year, day_of_year = map(int, ordinal_match.groups())
@@ -809,8 +798,19 @@ def parse_time_string(time_str, freq=None):
809798
start_q, end_q = multi_quarter_match.groups()
810799
return pd.period_range(start=start_q, end=end_q, freq="Q")
811800

801+
elif week_match:
802+
start_date, end_date = week_match.groups()
803+
start = pd.Timestamp(start_date)
804+
end = pd.Timestamp(end_date)
805+
806+
# Ensure the range actually covers a full week (7 days)
807+
if (end - start).days == 6:
808+
return pd.Period(start, freq="W")
809+
810+
812811
return None # No match found
813812

813+
814814
# ----------------------------------------------------------------------
815815
# Parsing for type-inference
816816

0 commit comments

Comments
 (0)