Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow for to_datetime / strftime to automatically parse dates with single-digit hour/minute/second #20144

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading