Skip to content

BUG/API: Timestamp(naive_str, tz=tz) vs Timestamp(naive_str).tz_localize(tz) #55657

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

Closed
jbrockmendel opened this issue Oct 24, 2023 · 3 comments · Fixed by #55712
Closed

BUG/API: Timestamp(naive_str, tz=tz) vs Timestamp(naive_str).tz_localize(tz) #55657

jbrockmendel opened this issue Oct 24, 2023 · 3 comments · Fixed by #55712
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member Timestamp pd.Timestamp and associated methods

Comments

@jbrockmendel
Copy link
Member

jbrockmendel commented Oct 24, 2023

dtstr = '11/06/2011 01:00'
tz = "US/Eastern"

>>> pd.Timestamp(dtstr, tz=tz)
Timestamp('2011-11-06 01:00:00-0500', tz='US/Eastern')

>>> pd.Timestamp(dtstr).tz_localize(tz)
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from 2011-11-06 01:00:00, try using the 'ambiguous' argument

I expected these to be equivalent. Am I wrong to expect that?

Looks like the constructor version goes through _localize_pydatetime (then pytz's tz.localize) instead of Timestamp.tz_localize which apparently doesn't find this case ambiguous.

Update: passing is_dst=None to tz.localize in _localize_pydatetime causes the constructor version to raise like I expected.

Update 2: passing is_dst=None as above breaks two tests in the test suite: test_dti_tz_localize_ambiguous_infer and test_fallback_singular

@jbrockmendel jbrockmendel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 24, 2023
@jbrockmendel
Copy link
Member Author

@jreback thoughts on the original intention here?

@jbrockmendel
Copy link
Member Author

Discused with @jorisvandenbossche and @mroeschke today on the dev call. Agreed that we expect the constructor to behave like tz_localize. Noted that using ZoneInfo (and probably dateutil) instead of pytz tzinfo has different behavior bc of 'fold' keyword, which we don't handle well in the Timestamp constructor.

@jorisvandenbossche
Copy link
Member

Agreed that we expect the constructor to behave like tz_localize

Or that we expect it to honor the fold keyword?

@jbrockmendel jbrockmendel added the Timestamp pd.Timestamp and associated methods label Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member Timestamp pd.Timestamp and associated methods
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants