Skip to content

Commit

Permalink
feat: Allow for to_datetime / strftime to automatically parse dat…
Browse files Browse the repository at this point in the history
…es with single-digit hour/minute/second (#20144)
  • Loading branch information
wsyxbcl authored Dec 6, 2024
1 parent 36b1244 commit 370a02e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions crates/polars-time/src/chunkedarray/string/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)?
Expand All @@ -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
)?
Expand Down
16 changes: 16 additions & 0 deletions py-polars/tests/unit/operations/namespaces/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down

0 comments on commit 370a02e

Please sign in to comment.