-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
handle DST appropriately in Timestamp.replace #18618
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cfb760c
handle DST appropriately in Timestamp.replace
jbrockmendel 4ca60fd
better comment, move test to test_timezones
jbrockmendel f738989
informative comment in test
jbrockmendel 98a7296
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 6f36a97
handle DST appropriately in Timestamp.replace
jbrockmendel b916b17
better comment, move test to test_timezones
jbrockmendel b00d763
informative comment in test
jbrockmendel 000f6cd
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel ec9b38a
Merge branch 'replace_bug' of https://github.com/jbrockmendel/pandas …
jbrockmendel f5886e2
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 4cfd949
fix duplicate whatsnews caused by merge mixups
jbrockmendel a3b14f9
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 504cf97
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 4459c9f
fixup merge messup
jbrockmendel 1eb5f2f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel dced361
refactor test to apply to both pytz and dateutil
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1228,6 +1228,27 @@ def f(): | |
dt = Timestamp('2013-11-03 01:59:59.999999-0400', tz='US/Eastern') | ||
assert dt.tz_localize(None) == dt.replace(tzinfo=None) | ||
|
||
def test_replace_across_dst(self): | ||
# GH#18319 check that 1) timezone is correctly normalized and | ||
# 2) that hour is not incorrectly changed by this normalization | ||
tz = pytz.timezone('US/Eastern') | ||
|
||
ts_naive = Timestamp('2017-12-03 16:03:30') | ||
ts_aware = tz.localize(ts_naive) | ||
|
||
# Preliminary sanity-check | ||
assert ts_aware == ts_aware.tzinfo.normalize(ts_aware) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to parametrize this across dateutil & pytz, so you need to move this to |
||
# Replace across DST boundary | ||
ts2 = ts_aware.replace(month=6) | ||
|
||
# Check that `replace` preserves hour literal | ||
assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute) | ||
|
||
# Check that post-replace object is appropriately normalized | ||
ts2b = ts2.tzinfo.normalize(ts2) | ||
assert ts2 == ts2b | ||
|
||
def test_ambiguous_compat(self): | ||
# validate that pytz and dateutil are compat for dst | ||
# when the transition happens | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you tests with dateutil as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is pretty specific to pytz API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is specific to the
pytz
API because the problem is specific to thepytz
API, so you can't write tests that currently fail withdateutil
, but you can, in fact, write this test to be agnostic as to whether the test takes apytz
ordateutil
zone.Rather than using
localize
andnormalize
, express the "expected" datetimes as UTC datetimes and use.astimezone
.pytz
anddateutil
supportastimezone
(and, in fact,pytz.normalize
essentially just uses.astimezone
under the hood). Sincepandas
provides their own API layer on top ofdatetime
, it is also agnostic to the timezone provider.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a suggestion for this test or more generally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this test at least, since with tests you can always engineer your datetime literals such that you never have to use anything except
astimezone
.It's also just a useful piece of information to have, that
astimezone
is one of the few timezone functions wherepytz
behaves more or less nicely. You only neednormalize
semantics withpytz
for things likereplace
or "calendar" arithmetic, where you want to change the naive portion of the datetime to a specific value and then change the time zone offset to match. If you want "absolute" arithmetic (where you want to go forward a certain number of hours or seconds or something), thenthe_operation(dt.astimezone(UTC)).astimezone(dt.tzinfo)
will always give the right answer.In this case you know what the correct answer should be, in absolute time, so you can just declare your initial variable as the correct answer in
UTC
and convert that to the time zone you care about. If you do it generically like that, you are insulated from any weird quirks ofpytz
's interface, and you getdateutil
support for free (so you can parametrize this test usingpytz
anddateutil
zones).Of course, this method will fail if you try to construct an imaginary time, since there is no mapping between UTC and imaginary times.