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

BUG: calling Timestamp constructor on Timestamp at DST changes Timestamp value #30692

Closed
AlexKirko opened this issue Jan 4, 2020 · 1 comment

Comments

@AlexKirko
Copy link
Member

Code Sample, a copy-pastable example if possible


tz = "dateutil/Europe/London"
ts = pd.Timestamp("2013-10-27 01:00:00")
print("Initial value is {}".format(ts.value))
ts = ts.tz_localize(tz, ambiguous=False, nonexistent="raise")
print("After localization value is {}".format(ts.value))
ts = pd.Timestamp(ts)
print("After casting Timestamp as Timestamp value is {}".format(ts.value))

Output:
0.26.0.dev0+1608.g06c5d2488

Initial value is 1382835600000000000
After localization value is 1382835600000000000
After casting Timestamp as Timestamp value is 1382832000000000000

Problem description

TLDR: Calling the Timestamp constructor on a Timestamp shouldn't change the object in any way.

Long version: Ran into this while working on solving #30543. When we localize into a Daylight Savings Time timezone, we are forced to alter the underlying value of a Timestamp to make it fit non-DST timezones. WIthout this, date arithmetic won't work properly between timezones. This is also necessary, for example, to make sure that a pd.date_range with one of the ends on a DST shift doesn't break (the test for this is implemented by test_dti_construction_ambiguous_endpoint in pandas.tests.indexes.datetimes.test_timezones.TestDatetimeIndexTimezones). Unfortunately, what happens currently is that ts.tz_localize doesn't change the underlying value on a DST shift. It instead changes when the Timestamp constructor is called on a localized Timestamp (you can take a look, for example, here).

Expected Output

Initial value is 1382835600000000000
After localization value is 1382832000000000000
After casting Timestamp as Timestamp value is 1382832000000000000

The value should change on localization, not on calling the constructor.
I'd like to work on a PR, because without fixing this it's impossible to solve #30543 without introducing ugly hacks.

xref: #30543

@mroeschke
Copy link
Member

Thanks for the report. I think this is a duplicate of #24329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants