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

Disallow with_time_zone on tz-naive #6658

Closed
MarcoGorelli opened this issue Feb 3, 2023 · 0 comments · Fixed by #6659
Closed

Disallow with_time_zone on tz-naive #6658

MarcoGorelli opened this issue Feb 3, 2023 · 0 comments · Fixed by #6659
Labels
enhancement New feature or an improvement of an existing feature

Comments

@MarcoGorelli
Copy link
Collaborator

Problem description

Breaking this off from #6561

with_time_zone on a tz-naive converts as if it were starting from UTC:

In [8]: tz_naive
Out[8]:
shape: (1,)
Series: '' [datetime[μs]]
[
        2020-01-01 03:00:00
]

In [9]: tz_naive.dt.with_time_zone('America/Barbados')
Out[9]:
shape: (1,)
Series: '' [datetime[μs, America/Barbados]]
[
        2019-12-31 23:00:00 AST
]

I think this is rarely what people are actually looking for - instead, they probably wanted tz_localize / cast_time_zone:

In [10]: tz_naive.dt.tz_localize('America/Barbados')
Out[10]:
shape: (1,)
Series: '' [datetime[μs, America/Barbados]]
[
        2020-01-01 03:00:00 AST
]

So, I think it would be more user-friendly to raise an error, leading people towards cast_time_zone


Having looked into this further, I think this might have been the original intention in https://github.com/pola-rs/polars/pull/4645/files, and that with_time_zone on tz-naive might only be working accidentallly:

dt => Err(PolarsError::ComputeError(
format!("Series of dtype {dt:?} has got no time zone").into(),
)),

@ritchie46 could you confirm please? (and apologies if I misread your original intention!)

@MarcoGorelli MarcoGorelli added the enhancement New feature or an improvement of an existing feature label Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant