diff --git a/crates/polars-time/src/chunkedarray/string/infer.rs b/crates/polars-time/src/chunkedarray/string/infer.rs index 03d0ae15c73b..ea5be7b112a1 100644 --- a/crates/polars-time/src/chunkedarray/string/infer.rs +++ b/crates/polars-time/src/chunkedarray/string/infer.rs @@ -21,12 +21,12 @@ const DATETIME_DMY_PATTERN: &str = r#"(?x) (?:\d{4,}) # year (?: [T\ ] # separator - (?:\d{2}) # hour + (?:\d{1,2}) # hour :? # separator - (?:\d{2}) # minute + (?:\d{1,2}) # minute (?: :? # separator - (?:\d{2}) # second + (?:\d{1,2}) # second (?: \.(?:\d{1,9}) # subsecond )? @@ -47,12 +47,12 @@ const DATETIME_YMD_PATTERN: &str = r#"(?x) (?:\d{1,2}) # day (?: [T\ ] # separator - (?:\d{2}) # hour + (?:\d{1,2}) # hour :? # separator - (?:\d{2}) # minute + (?:\d{1,2}) # minute (?: :? # separator - (?:\d{2}) # seconds + (?:\d{1,2}) # seconds (?: \.(?:\d{1,9}) # subsecond )? diff --git a/py-polars/tests/unit/operations/namespaces/test_strptime.py b/py-polars/tests/unit/operations/namespaces/test_strptime.py index 41fdb028e31d..827b9b9811bb 100644 --- a/py-polars/tests/unit/operations/namespaces/test_strptime.py +++ b/py-polars/tests/unit/operations/namespaces/test_strptime.py @@ -151,6 +151,22 @@ def test_to_date_all_inferred_date_patterns(time_string: str, expected: date) -> assert result[0] == expected +@pytest.mark.parametrize( + ("time_string", "expected"), + [ + ("2024-12-04 09:08:00", datetime(2024, 12, 4, 9, 8, 0)), + ("2024-12-4 9:8:0", datetime(2024, 12, 4, 9, 8, 0)), + ("2024/12/04 9:8", datetime(2024, 12, 4, 9, 8, 0)), + ("4/12/2024 9:8", datetime(2024, 12, 4, 9, 8, 0)), + ], +) +def test_to_datetime_infer_missing_digit_in_time_16092( + time_string: str, expected: datetime +) -> None: + result = pl.Series([time_string]).str.to_datetime() + assert result[0] == expected + + @pytest.mark.parametrize( ("value", "attr"), [