Skip to content

Commit f7979be

Browse files
author
MarcoGorelli
committed
move format_is_iso to strptime
1 parent 2f8fade commit f7979be

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

pandas/_libs/tslibs/parsing.pyi

-1
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

-20
Original file line numberDiff line numberDiff line change
@@ -839,26 +839,6 @@ class _timelex:
839839
_DATEUTIL_LEXER_SPLIT = _timelex.split
840840

841841

842-
def format_is_iso(f: str) -> bint:
843-
"""
844-
Does format match the iso8601 set that can be handled by the C parser?
845-
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
846-
but must be consistent. Leading 0s in dates and times are optional.
847-
"""
848-
iso_template = "%Y{date_sep}%m{date_sep}%d{time_sep}%H:%M:%S{micro_or_tz}".format
849-
excluded_formats = ["%Y%m"]
850-
851-
for date_sep in [" ", "/", "\\", "-", ".", ""]:
852-
for time_sep in [" ", "T"]:
853-
for micro_or_tz in ["", "%z", ".%f", ".%f%z"]:
854-
if (iso_template(date_sep=date_sep,
855-
time_sep=time_sep,
856-
micro_or_tz=micro_or_tz,
857-
).startswith(f) and f not in excluded_formats):
858-
return True
859-
return False
860-
861-
862842
def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
863843
"""
864844
Guess the datetime format of a given datetime string.

pandas/_libs/tslibs/strptime.pxd

+1
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

+20-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ from pandas._libs.tslibs.timestamps import Timestamp
5353

5454
cnp.import_array()
5555

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

pandas/tests/tslibs/test_parsing.py

+5-2
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

@@ -306,7 +309,7 @@ def test_parse_time_string_check_instance_type_raise_exception():
306309
)
307310
def test_is_iso_format(fmt, expected):
308311
# see gh-41047
309-
result = parsing.format_is_iso(fmt)
312+
result = strptime.format_is_iso(fmt)
310313
assert result == expected
311314

312315

0 commit comments

Comments
 (0)