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(rust, python): disallow with_time_zone from/to tz-naive #6659

Merged
merged 7 commits into from
Feb 4, 2023

Conversation

MarcoGorelli
Copy link
Collaborator

closes #6658

@MarcoGorelli MarcoGorelli changed the title Stricter with time zone feat(rust, python): disallow with_time_zone from/to tz-naive Feb 3, 2023
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Feb 3, 2023
@ritchie46
Copy link
Member

Thanks for the PR, Could you help me again in the rationale of this? I don't fully understand.

@MarcoGorelli
Copy link
Collaborator Author

MarcoGorelli commented Feb 3, 2023

Sure! It's that if people start with a tz-naive datetime, and they want to set a timezone, then they're probably not looking at doing any conversion - they just want to set the time zone

I can imagine many users doing something like:

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
]

# "hmm wait, that's not what I wanted..."

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

# "ah, that's more like it"

And that's if they look at their output - if they assume that the timezone was set, then it's potentially a hidden bug.

If someone really wants a conversion from UTC, then I think it's better to force them to be explicit about it, and write

tz_naive.dt.cast_time_zone('UTC').dt.with_time_zone('America/Barbados')`

Like this, teaching time zones would be easy:

  • with cast_time_zone, you set/unset/change the timezone
  • with with_time_zone, you convert from one timezone to another

Whereas currently it's a bit more confusing, because with_time_zone also converts from naive as if it were in UTC


For comparison, pandas disallows tz_convert on tz-naive:

In [8]: ser.dt.tz_convert('America/Barbados')
---------------------------------------------------------------------------
TypeError: Cannot convert tz-naive timestamps, use tz_localize to localize

@ritchie46
Copy link
Member

Right, makes sense. Thanks!

@ritchie46 ritchie46 merged commit 0d5441f into pola-rs:master Feb 4, 2023
Vincenthays pushed a commit to Vincenthays/polars that referenced this pull request Feb 9, 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 python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow with_time_zone on tz-naive
2 participants