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 16 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
34 changes: 25 additions & 9 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from cpython.datetime cimport (
import_datetime()

from _thread import allocate_lock as _thread_allocate_lock
import re

import numpy as np
import pytz
Expand Down Expand Up @@ -43,13 +44,16 @@ from pandas._libs.tslibs.np_datetime cimport (
pydatetime_to_dt64,
string_to_dts,
)

from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

Copy link
Member

Choose a reason for hiding this comment

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

could you revert these unrelated changes please?

from pandas._libs.tslibs.timestamps cimport _Timestamp
from pandas._libs.util cimport (
is_datetime64_object,
is_float_object,
is_integer_object,
)

from pandas._libs.tslibs.timestamps import Timestamp

cnp.import_array()
Expand All @@ -60,15 +64,27 @@ cdef bint format_is_iso(f: str):
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.
"""
excluded_formats = ["%Y%m"]

for date_sep in [" ", "/", "\\", "-", ".", ""]:
for time_sep in [" ", "T"]:
for micro_or_tz in ["", "%z", ".%f", ".%f%z"]:
iso_fmt = f"%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}"
if iso_fmt.startswith(f) and f not in excluded_formats:
return True
return False
iso_regex = re.compile(
r"""
^ # start of string
%Y # Year
(?:([-/ \\.]?)%m # month with or without separators
(?:\1%d # day with or without separators
Copy link
Member

Choose a reason for hiding this comment

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

perhaps let's update the comment to "day with same separator as for year-month"?

(?:[ |T]%H # hour with separator
(?:\:%M # minute with separator
(?:\:%S # second with separator
(?:%z|.%f(?:%z|Z)? # timezone or fractional second
)?)?)?)?)?)? # optional
$ # end of string
""",
re.VERBOSE,
)
excluded_formats = [
r"^%Y%m$",
]
if any(re.match(pattern, f) for pattern in excluded_formats):
return False
return bool(re.match(iso_regex, f))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need a regex for the excluded formats - would

return re.match(iso_regex, f) is not None and f not in excluded_formats

work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea worked fine without the regex excluded_format



def _test_format_is_iso(f: str) -> bool:
Expand Down