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

fix(rust): auto-detect %+ as tz-aware #6434

Merged
merged 1 commit into from
Jan 25, 2023
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
2 changes: 1 addition & 1 deletion polars/polars-time/src/chunkedarray/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pub trait Utf8Methods: AsUtf8 {
None => return infer::to_datetime(utf8_ca, tu),
};
// todo! use regex?
if fmt.contains("%z") || fmt.contains("%:z") || fmt.contains("%#z") {
if fmt.contains("%z") || fmt.contains("%:z") || fmt.contains("%#z") || fmt == "%+" {
tz_aware = true;
}
let fmt = self::strptime::compile_fmt(fmt);
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/internals/expr/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def strptime(
Format to use, refer to the `chrono strftime documentation
<https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
for specification. Example: ``"%y-%m-%d"``.
Note that the ``Z`` suffix for "Zulu time" is not (yet!) supported:
you should instead insert a ``Z`` in your ``fmt`` string and then
use ``dt.with_time_zone``.
Note that the ``Z`` suffix for "Zulu time" in ISO8601 formats is not (yet!)
fully supported: you should first try ``"%+"`` and if that fails, insert a
``Z`` in your ``fmt`` string and then use ``dt.with_time_zone``.
strict
Raise an error if any conversion fails.
exact
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/internals/series/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def strptime(
`chrono strftime documentation
<https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
for specification. Example: ``"%y-%m-%d"``.
Note that the ``Z`` suffix for "Zulu time" is not (yet!) supported:
you should instead insert a ``Z`` in your ``fmt`` string and then
use ``dt.with_time_zone``.
Note that the ``Z`` suffix for "Zulu time" in ISO8601 formats is not (yet!)
fully supported: you should first try ``"%+"`` and if that fails, insert a
``Z`` in your ``fmt`` string and then use ``dt.with_time_zone``.
strict
Raise an error if any conversion fails.
exact
Expand Down
29 changes: 27 additions & 2 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io
import sys
from datetime import date, datetime, time, timedelta
from datetime import date, datetime, time, timedelta, timezone
from typing import TYPE_CHECKING, cast, no_type_check

import numpy as np
Expand Down Expand Up @@ -2282,6 +2282,32 @@ def test_tz_aware_truncate() -> None:
}


@pytest.mark.parametrize(
("ts", "fmt", "expected"),
[
("2020-01-01T00:00:00Z", "%+", datetime(2020, 1, 1, tzinfo=timezone.utc)),
(
"2020-01-01T00:00:00+01:00",
"%Y-%m-%dT%H:%M:%S%z",
datetime(2020, 1, 1, tzinfo=timezone(timedelta(seconds=3600))),
),
(
"2020-01-01T00:00:00+01:00",
"%Y-%m-%dT%H:%M:%S%:z",
datetime(2020, 1, 1, tzinfo=timezone(timedelta(seconds=3600))),
),
(
"2020-01-01T00:00:00+01:00",
"%Y-%m-%dT%H:%M:%S%#z",
datetime(2020, 1, 1, tzinfo=timezone(timedelta(seconds=3600))),
),
],
)
def test_tz_aware_strptime(ts: str, fmt: str, expected: datetime) -> None:
result = pl.Series([ts]).str.strptime(pl.Datetime, fmt).item()
assert result == expected


def test_tz_aware_strftime() -> None:
df = pl.DataFrame(
{
Expand Down Expand Up @@ -2600,7 +2626,6 @@ def test_infer_iso8601(iso8601_format: str) -> None:
.replace("%3f", "123")
.replace("%6f", "123456")
.replace("%9f", "123456789")
.replace("%Z", "Z")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not strictly related to this PR, but the infer_pattern_datetime_single doesn't handle timezone-aware (yet?), so this line wasn't necessary

)
parsed = pl.Series([time_string]).str.strptime(pl.Datetime("ns"))
assert parsed.dt.year().item() == 2134
Expand Down