Skip to content

used regular expression in format_is_iso() #50468

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

Merged
Merged
Changes from 6 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
25 changes: 15 additions & 10 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -824,17 +824,22 @@ def format_is_iso(f: str) -> bint:
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
but must be consistent. Leading 0s in dates and times are optional.
"""
iso_template = "%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}".format
iso_regex = re.compile(
r"""
^ # start of string
\d{4} # match a 4-digit year
([ -/\\.]\d{2}|\d{2})? # optionally match a 2-digit month
([ -/\\.]\d{2}|\d{2})? # optionally match a 2-digit day
([ T]\d{2}:\d{2}:\d{2} # match time in the format "THH:MM:SS"
(\.\d+)? # optionally match a decimal and fractional seconds
([+-]\d{2}:\d{2}|Z)?)? # optional match timezone in the format "+HH:MM" or "Z"
$ # end of string
""",
re.VERBOSE,
)
excluded_formats = ["%Y%m%d", "%Y%m", "%Y"]

for date_sep in [" ", "/", "\\", "-", ".", ""]:
for time_sep in [" ", "T"]:
for micro_or_tz in ["", "%z", ".%f", ".%f%z"]:
if (iso_template(date_sep=date_sep,
time_sep=time_sep,
micro_or_tz=micro_or_tz,
).startswith(f) and f not in excluded_formats):
return True
if re.match(iso_regex, f) and f not in excluded_formats:
return True
return False


Expand Down