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, python): raise if tz_localize called on UTC-aware #6526

Merged
merged 3 commits into from
Jan 29, 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
12 changes: 6 additions & 6 deletions polars/polars-lazy/polars-plan/src/dsl/function_expr/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ pub(super) fn cast_timezone(s: &Series, tz: &str) -> PolarsResult<Series> {
#[cfg(feature = "timezones")]
pub(super) fn tz_localize(s: &Series, tz: &str) -> PolarsResult<Series> {
let ca = s.datetime()?.clone();
match ca.time_zone() {
Some(tz) if tz == "UTC" => {
Ok(ca.with_time_zone(Some("UTC".into()))?.into_series())
}
Comment on lines -149 to -151
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was added in https://github.com/pola-rs/polars/pull/5522/files , but it looks like it's no longer necessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm maybe the intention was to match the target tz? I'll update later, marking as draft for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've rewritten according to what I think the intention was

Some(tz) if !tz.is_empty() => {
match (ca.time_zone(), tz) {
(Some(old_tz), _) if !old_tz.is_empty() => {
Err(PolarsError::ComputeError("Cannot localize a tz-aware datetime. Consider using 'dt.with_time_zone' or 'dt.cast_time_zone'".into()))
},
_ => {
(_, "UTC") => {
Ok(ca.with_time_zone(Some("UTC".into()))?.into_series())
}
(_, _) => {
Ok(ca.with_time_zone(Some("UTC".to_string()))?.cast_time_zone(tz)?.into_series())
}
}
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,21 @@ def test_tz_localize() -> None:
}


@pytest.mark.parametrize("time_zone", ["UTC", "Africa/Abidjan"])
def test_tz_localize_from_utc(time_zone: str) -> None:
ts_utc = (
pl.Series(["2018-10-28"]).str.strptime(pl.Datetime).dt.tz_localize(time_zone)
)
with pytest.raises(
ComputeError,
match=(
"^Cannot localize a tz-aware datetime. Consider using "
"'dt.with_time_zone' or 'dt.cast_time_zone'$"
),
):
ts_utc.dt.tz_localize("America/Maceio")


def test_tz_aware_truncate() -> None:
test = pl.DataFrame(
{
Expand Down