@@ -321,11 +321,10 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
321
321
322
322
dtarr = DatetimeArray ._simple_new (values , freq = freq , tz = tz )
323
323
result = object .__new__ (cls )
324
- result ._data = dtarr ._data
325
- result ._freq = dtarr .freq
326
- result ._tz = dtarr .tz
324
+ result ._eadata = dtarr
327
325
result .name = name
328
326
# For groupby perf. See note in indexes/base about _index_data
327
+ # TODO: make sure this is updated correctly if edited
329
328
result ._index_data = result ._data
330
329
result ._reset_identity ()
331
330
return result
@@ -345,19 +344,6 @@ def _values(self):
345
344
else :
346
345
return self .values
347
346
348
- @property
349
- def tz (self ):
350
- # GH 18595
351
- return self ._tz
352
-
353
- @tz .setter
354
- def tz (self , value ):
355
- # GH 3746: Prevent localizing or converting the index by setting tz
356
- raise AttributeError ("Cannot directly set timezone. Use tz_localize() "
357
- "or tz_convert() as appropriate" )
358
-
359
- tzinfo = tz
360
-
361
347
@property
362
348
def size (self ):
363
349
# TODO: Remove this when we have a DatetimeTZArray
@@ -416,15 +402,18 @@ def __setstate__(self, state):
416
402
data = np .empty (nd_state [1 ], dtype = nd_state [2 ])
417
403
np .ndarray .__setstate__ (data , nd_state )
418
404
405
+ freq = own_state [1 ]
406
+ tz = timezones .tz_standardize (own_state [2 ])
407
+ dtarr = DatetimeArray ._simple_new (data , freq = freq , tz = tz )
408
+
419
409
self .name = own_state [0 ]
420
- self ._freq = own_state [1 ]
421
- self ._tz = timezones .tz_standardize (own_state [2 ])
422
410
423
411
else : # pragma: no cover
424
412
data = np .empty (state )
425
413
np .ndarray .__setstate__ (data , state )
414
+ dtarr = DatetimeArray (data )
426
415
427
- self ._data = data
416
+ self ._eadata = dtarr
428
417
self ._reset_identity ()
429
418
430
419
else :
@@ -502,7 +491,9 @@ def union(self, other):
502
491
else :
503
492
result = Index .union (this , other )
504
493
if isinstance (result , DatetimeIndex ):
505
- result ._tz = timezones .tz_standardize (this .tz )
494
+ # TODO: we shouldn't be setting attributes like this;
495
+ # in all the tests this equality already holds
496
+ result ._eadata ._dtype = this .dtype
506
497
if (result .freq is None and
507
498
(this .freq is not None or other .freq is not None )):
508
499
result .freq = to_offset (result .inferred_freq )
@@ -530,11 +521,12 @@ def union_many(self, others):
530
521
if this ._can_fast_union (other ):
531
522
this = this ._fast_union (other )
532
523
else :
533
- tz = this .tz
524
+ dtype = this .dtype
534
525
this = Index .union (this , other )
535
526
if isinstance (this , DatetimeIndex ):
536
- this ._tz = timezones .tz_standardize (tz )
537
-
527
+ # TODO: we shouldn't be setting attributes like this;
528
+ # in all the tests this equality already holds
529
+ this ._eadata ._dtype = dtype
538
530
return this
539
531
540
532
def _can_fast_union (self , other ):
@@ -1129,9 +1121,20 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
1129
1121
# Wrapping DatetimeArray
1130
1122
1131
1123
@property
1132
- def _eadata (self ):
1133
- return DatetimeArray ._simple_new (self ._data ,
1134
- tz = self .tz , freq = self .freq )
1124
+ def _data (self ):
1125
+ return self ._eadata ._data
1126
+
1127
+ @property
1128
+ def tz (self ):
1129
+ # GH#18595
1130
+ return self ._eadata .tz
1131
+
1132
+ @tz .setter
1133
+ def tz (self , value ):
1134
+ # GH#3746; DatetimeArray will raise to disallow setting
1135
+ self ._eadata .tz = value
1136
+
1137
+ tzinfo = tz
1135
1138
1136
1139
# Compat for frequency inference, see GH#23789
1137
1140
_is_monotonic_increasing = Index .is_monotonic_increasing
@@ -1168,18 +1171,6 @@ def offset(self, value):
1168
1171
warnings .warn (msg , FutureWarning , stacklevel = 2 )
1169
1172
self .freq = value
1170
1173
1171
- @property
1172
- def freq (self ):
1173
- return self ._freq
1174
-
1175
- @freq .setter
1176
- def freq (self , value ):
1177
- if value is not None :
1178
- # let DatetimeArray to validation
1179
- self ._eadata .freq = value
1180
-
1181
- self ._freq = to_offset (value )
1182
-
1183
1174
def __getitem__ (self , key ):
1184
1175
result = self ._eadata .__getitem__ (key )
1185
1176
if is_scalar (result ):
0 commit comments