From ece698485e4e4d9838e5e1e0afc0065ad5f820ce Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Wed, 20 Sep 2023 12:12:17 -0400 Subject: [PATCH] Minimum digits in date - catch ValueError in parser --- xscen/catutils.py | 5 ++++- xscen/utils.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/xscen/catutils.py b/xscen/catutils.py index 9ec9c36b..d96cd7ce 100644 --- a/xscen/catutils.py +++ b/xscen/catutils.py @@ -90,7 +90,10 @@ def _parse_level(text: str) -> str: return text -@register_parse_type("datebounds", regex=r"(([\d]+(\-[\d]+)?)|fx)", group_count=3) +# Minimum 4 digits for a date (a single year). Maximum is, in theory, YYYYMMDDHHMMSS so 14. +@register_parse_type( + "datebounds", regex=r"(([\d]{4,15}(\-[\d]{4,15})?)|fx)", group_count=3 +) def _parse_datebounds(text: str) -> tuple[str, str]: """Parse helper to translate date bounds, used in the special DATES field.""" if "-" in text: diff --git a/xscen/utils.py b/xscen/utils.py index b4cfb0f7..04c67b4f 100644 --- a/xscen/utils.py +++ b/xscen/utils.py @@ -107,7 +107,7 @@ def _parse_date(date, fmts): except (KeyError, ValueError): try: date = pd.Timestamp(date) - except pd._libs.tslibs.parsing.DateParseError: + except (pd._libs.tslibs.parsing.DateParseError, ValueError): date = pd.NaT elif isinstance(date, cftime.datetime): for n in range(3):