Skip to content

Commit 89a018e

Browse files
authored
ENH: guess ISO8601 formats without separators (#50622)
expand guess_datetime_format a bit Co-authored-by: MarcoGorelli <>
1 parent f12e2a7 commit 89a018e

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

pandas/_libs/tslibs/parsing.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,12 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
848848

849849
# attr name, format, padding (if any)
850850
datetime_attrs_to_format = [
851+
(("year", "month", "day", "hour", "minute", "second"), "%Y%m%d%H%M%S", 0),
852+
(("year", "month", "day", "hour", "minute"), "%Y%m%d%H%M", 0),
853+
(("year", "month", "day", "hour"), "%Y%m%d%H", 0),
851854
(("year", "month", "day"), "%Y%m%d", 0),
855+
(("hour", "minute", "second"), "%H%M%S", 0),
856+
(("hour", "minute"), "%H%M", 0),
852857
(("year",), "%Y", 0),
853858
(("month",), "%B", 0),
854859
(("month",), "%b", 0),

pandas/tests/tools/test_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2893,8 +2893,8 @@ class TestDatetimeParsingWrappers:
28932893
("2014-6", datetime(2014, 6, 1), None),
28942894
("6-2014", datetime(2014, 6, 1), UserWarning),
28952895
("20010101 12", datetime(2001, 1, 1, 12), None),
2896-
("20010101 1234", datetime(2001, 1, 1, 12, 34), UserWarning),
2897-
("20010101 123456", datetime(2001, 1, 1, 12, 34, 56), UserWarning),
2896+
("20010101 1234", datetime(2001, 1, 1, 12, 34), None),
2897+
("20010101 123456", datetime(2001, 1, 1, 12, 34, 56), None),
28982898
],
28992899
)
29002900
def test_parsers(self, date_str, expected, warning, cache):

pandas/tests/tslibs/test_parsing.py

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def test_parsers_month_freq(date_str, expected):
149149
"string,fmt",
150150
[
151151
("20111230", "%Y%m%d"),
152+
("201112300000", "%Y%m%d%H%M"),
153+
("20111230000000", "%Y%m%d%H%M%S"),
154+
("20111230T00", "%Y%m%dT%H"),
155+
("20111230T0000", "%Y%m%dT%H%M"),
156+
("20111230T000000", "%Y%m%dT%H%M%S"),
152157
("2011-12-30", "%Y-%m-%d"),
153158
("2011", "%Y"),
154159
("2011-01", "%Y-%m"),

0 commit comments

Comments
 (0)