Skip to content

Commit 794f9e4

Browse files
author
MarcoGorelli
committed
move format_is_iso to strptime
1 parent 3490468 commit 794f9e4

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

pandas/_libs/tslibs/parsing.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def try_parse_datetime_components(
4040
minutes: npt.NDArray[np.object_], # object[:]
4141
seconds: npt.NDArray[np.object_], # object[:]
4242
) -> npt.NDArray[np.object_]: ...
43-
def format_is_iso(f: str) -> bool: ...
4443
def guess_datetime_format(
4544
dt_str,
4645
dayfirst: bool | None = ...,

pandas/_libs/tslibs/parsing.pyx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -818,26 +818,6 @@ class _timelex:
818818
_DATEUTIL_LEXER_SPLIT = _timelex.split
819819

820820

821-
def format_is_iso(f: str) -> bint:
822-
"""
823-
Does format match the iso8601 set that can be handled by the C parser?
824-
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
825-
but must be consistent. Leading 0s in dates and times are optional.
826-
"""
827-
iso_template = "%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}".format
828-
excluded_formats = ["%Y%m"]
829-
830-
for date_sep in [" ", "/", "\\", "-", ".", ""]:
831-
for time_sep in [" ", "T"]:
832-
for micro_or_tz in ["", "%z", ".%f", ".%f%z"]:
833-
if (iso_template(date_sep=date_sep,
834-
time_sep=time_sep,
835-
micro_or_tz=micro_or_tz,
836-
).startswith(f) and f not in excluded_formats):
837-
return True
838-
return False
839-
840-
841821
def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
842822
"""
843823
Guess the datetime format of a given datetime string.

pandas/_libs/tslibs/strptime.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from numpy cimport int64_t
22

33

4+
cpdef bint format_is_iso(str f)
45
cdef bint parse_today_now(str val, int64_t* iresult, bint utc)

pandas/_libs/tslibs/strptime.pyx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ from pandas._libs.tslibs.timestamps import Timestamp
5454

5555
cnp.import_array()
5656

57+
cpdef bint format_is_iso(f: str):
58+
"""
59+
Does format match the iso8601 set that can be handled by the C parser?
60+
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
61+
but must be consistent. Leading 0s in dates and times are optional.
62+
"""
63+
iso_template = "%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}".format
64+
excluded_formats = ["%Y%m"]
65+
66+
for date_sep in [" ", "/", "\\", "-", ".", ""]:
67+
for time_sep in [" ", "T"]:
68+
for micro_or_tz in ["", "%z", ".%f", ".%f%z"]:
69+
if (iso_template(date_sep=date_sep,
70+
time_sep=time_sep,
71+
micro_or_tz=micro_or_tz,
72+
).startswith(f) and f not in excluded_formats):
73+
return True
74+
return False
75+
76+
5777
cdef bint parse_today_now(str val, int64_t* iresult, bint utc):
5878
# We delay this check for as long as possible
5979
# because it catches relatively rare cases
@@ -115,8 +135,6 @@ def array_strptime(
115135
exact : matches must be exact if True, search if False
116136
errors : string specifying error handling, {'raise', 'ignore', 'coerce'}
117137
"""
118-
from pandas._libs.tslibs.parsing import format_is_iso
119-
120138
cdef:
121139
Py_ssize_t i, n = len(values)
122140
npy_datetimestruct dts

pandas/tests/tslibs/test_parsing.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import numpy as np
99
import pytest
1010

11-
from pandas._libs.tslibs import parsing
11+
from pandas._libs.tslibs import (
12+
parsing,
13+
strptime,
14+
)
1215
from pandas._libs.tslibs.parsing import parse_time_string
1316
import pandas.util._test_decorators as td
1417

@@ -312,7 +315,7 @@ def test_parse_time_string_check_instance_type_raise_exception():
312315
)
313316
def test_is_iso_format(fmt, expected):
314317
# see gh-41047
315-
result = parsing.format_is_iso(fmt)
318+
result = strptime.format_is_iso(fmt)
316319
assert result == expected
317320

318321

0 commit comments

Comments
 (0)