Skip to content

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 24 commits into from
Apr 11, 2018
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b878540
DOC: update the Index.isin docstring (#20249)
noemielteto Mar 18, 2018
0b0fb83
MAINT: Remove weird pd file
gfyoung Mar 19, 2018
8316501
BUG: Retain timezone when resampling
mroeschke Mar 21, 2018
0bb1b13
Normalize pytz timezone for Datetimeindexes
mroeschke Mar 22, 2018
4d6f0d1
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 23, 2018
4a202b0
Change construction of tz
mroeschke Mar 26, 2018
29560c4
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 27, 2018
87cacf8
lint and add whatsnew
mroeschke Mar 27, 2018
e8990fc
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 28, 2018
b1f2724
Adjust whatsnew and add additional test
mroeschke Mar 28, 2018
c1241f9
Adjust test
mroeschke Mar 28, 2018
9833f01
Address review and failing CI test
mroeschke Mar 29, 2018
43fab89
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 29, 2018
f1a5ca7
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 30, 2018
bba5da5
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 31, 2018
8b397d4
Remove extra copy
mroeschke Mar 31, 2018
464a91b
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Mar 31, 2018
c1db598
add same tz error to timestamp
mroeschke Mar 31, 2018
bf1ec9e
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Apr 2, 2018
81ccb21
Add description of issue
mroeschke Apr 2, 2018
12f697b
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Apr 3, 2018
867ef19
Merge remote-tracking branch 'upstream/master' into resample_timezone
mroeschke Apr 4, 2018
360c295
Use cache_readonly
mroeschke Apr 4, 2018
67a29d5
standardize ._tz directly
mroeschke Apr 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
standardize ._tz directly
  • Loading branch information
mroeschke committed Apr 6, 2018
commit 67a29d54d0df33977a9cb4bd24b974e240073aa3
19 changes: 13 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
result.name = name
result.offset = freq
result._tz = timezones.maybe_get_tz(tz)
result._tz = timezones.tz_standardize(result._tz)
result._reset_identity()
return result

Expand Down Expand Up @@ -678,10 +679,16 @@ def _values(self):
else:
return self.values

@cache_readonly
@property
def tz(self):
# GH 18595
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe we now have this in setter / getter versions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the only caching decorator I could find was implemented here:

cdef class CachedProperty(object):
cdef readonly:
object func, name, __doc__
def __init__(self, func):
self.func = func
self.name = func.__name__
self.__doc__ = getattr(func, '__doc__', None)
def __get__(self, obj, typ):
if obj is None:
# accessed on the class, not the instance
return self
# Get the cache or set a default one if needed
cache = getattr(obj, '_cache', None)
if cache is None:
try:
cache = obj._cache = {}
except (AttributeError):
return self
if PyDict_Contains(cache, self.name):
# not necessary to Py_INCREF
val = <object> PyDict_GetItem(cache, self.name)
else:
val = self.func(obj)
PyDict_SetItem(cache, self.name, val)
return val
def __set__(self, obj, value):
raise AttributeError("Can't set attribute")
cache_readonly = CachedProperty

return timezones.tz_standardize(self._tz)
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):
Expand Down Expand Up @@ -753,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:
Expand Down Expand Up @@ -830,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]':
Expand Down Expand Up @@ -1174,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)
Expand Down Expand Up @@ -1222,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)
Expand Down