-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API/BUG: Enforce "normalized" pytz timezones for DatetimeIndex #20510
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 all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b878540
DOC: update the Index.isin docstring (#20249)
noemielteto 0b0fb83
MAINT: Remove weird pd file
gfyoung 8316501
BUG: Retain timezone when resampling
mroeschke 0bb1b13
Normalize pytz timezone for Datetimeindexes
mroeschke 4d6f0d1
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 4a202b0
Change construction of tz
mroeschke 29560c4
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 87cacf8
lint and add whatsnew
mroeschke e8990fc
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke b1f2724
Adjust whatsnew and add additional test
mroeschke c1241f9
Adjust test
mroeschke 9833f01
Address review and failing CI test
mroeschke 43fab89
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke f1a5ca7
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke bba5da5
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 8b397d4
Remove extra copy
mroeschke 464a91b
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke c1db598
add same tz error to timestamp
mroeschke bf1ec9e
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 81ccb21
Add description of issue
mroeschke 12f697b
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 867ef19
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke 360c295
Use cache_readonly
mroeschke 67a29d5
standardize ._tz directly
mroeschke 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
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 |
---|---|---|
|
@@ -511,13 +511,7 @@ def _generate(cls, start, end, periods, name, offset, | |
'different timezones') | ||
|
||
inferred_tz = timezones.maybe_get_tz(inferred_tz) | ||
|
||
# these may need to be localized | ||
tz = timezones.maybe_get_tz(tz) | ||
if tz is not None: | ||
date = start or end | ||
if date.tzinfo is not None and hasattr(tz, 'localize'): | ||
tz = tz.localize(date.replace(tzinfo=None)).tzinfo | ||
|
||
if tz is not None and inferred_tz is not None: | ||
if not timezones.tz_compare(inferred_tz, tz): | ||
|
@@ -654,7 +648,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, | |
result._data = values | ||
result.name = name | ||
result.offset = freq | ||
result.tz = timezones.maybe_get_tz(tz) | ||
result._tz = timezones.maybe_get_tz(tz) | ||
result._tz = timezones.tz_standardize(result._tz) | ||
result._reset_identity() | ||
return result | ||
|
||
|
@@ -684,6 +679,17 @@ def _values(self): | |
else: | ||
return self.values | ||
|
||
@property | ||
def tz(self): | ||
# GH 18595 | ||
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. same here |
||
return self._tz | ||
|
||
@tz.setter | ||
def tz(self, value): | ||
# GH 3746: Prevent localizing or converting the index by setting tz | ||
raise AttributeError("Cannot directly set timezone. Use tz_localize() " | ||
"or tz_convert() as appropriate") | ||
|
||
@property | ||
def tzinfo(self): | ||
""" | ||
|
@@ -754,7 +760,7 @@ def _cached_range(cls, start=None, end=None, periods=None, offset=None, | |
|
||
cachedRange = DatetimeIndex._simple_new(arr) | ||
cachedRange.offset = offset | ||
cachedRange.tz = None | ||
cachedRange = cachedRange.tz_localize(None) | ||
cachedRange.name = None | ||
drc[offset] = cachedRange | ||
else: | ||
|
@@ -831,7 +837,7 @@ def __setstate__(self, state): | |
|
||
self.name = own_state[0] | ||
self.offset = own_state[1] | ||
self.tz = own_state[2] | ||
self._tz = timezones.tz_standardize(own_state[2]) | ||
|
||
# provide numpy < 1.7 compat | ||
if nd_state[2] == 'M8[us]': | ||
|
@@ -1175,7 +1181,7 @@ def union(self, other): | |
else: | ||
result = Index.union(this, other) | ||
if isinstance(result, DatetimeIndex): | ||
result.tz = this.tz | ||
result._tz = timezones.tz_standardize(this.tz) | ||
if (result.freq is None and | ||
(this.freq is not None or other.freq is not None)): | ||
result.offset = to_offset(result.inferred_freq) | ||
|
@@ -1223,7 +1229,7 @@ def union_many(self, others): | |
tz = this.tz | ||
this = Index.union(this, other) | ||
if isinstance(this, DatetimeIndex): | ||
this.tz = tz | ||
this._tz = timezones.tz_standardize(tz) | ||
|
||
if this.freq is None: | ||
this.offset = to_offset(this.inferred_freq) | ||
|
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
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
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.
I generally avoid
hasattr
in projects that support Python 2.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.
yeah this impl is shared by DTI and TDI so this could be restructured a bit. Please file an issue .