2626import pandas .core .common as com
2727from pandas .core .indexes .base import Index
2828from pandas .core .indexes .datetimelike import (
29- DatetimeIndexOpsMixin , DatetimelikeDelegateMixin )
29+ DatetimeIndexOpsMixin , DatetimelikeDelegateMixin , ea_passthrough )
3030from pandas .core .indexes .numeric import Int64Index
3131from pandas .core .ops import get_op_result_name
3232import pandas .core .tools .datetimes as tools
@@ -96,19 +96,13 @@ class DatetimeDelegateMixin(DatetimelikeDelegateMixin):
9696 _delegate_class = DatetimeArray
9797
9898
99- @delegate_names (DatetimeArray , ["to_period" , "tz_localize" , "tz_convert" ,
100- "day_name" , "month_name" ],
101- typ = "method" , overwrite = True )
102- @delegate_names (DatetimeArray ,
103- DatetimeArray ._field_ops , typ = "property" , overwrite = True )
10499@delegate_names (DatetimeArray ,
105100 DatetimeDelegateMixin ._delegated_properties ,
106101 typ = "property" )
107102@delegate_names (DatetimeArray ,
108103 DatetimeDelegateMixin ._delegated_methods ,
109104 typ = "method" , overwrite = False )
110- class DatetimeIndex (DatetimeArray , DatetimeIndexOpsMixin , Int64Index ,
111- DatetimeDelegateMixin ):
105+ class DatetimeIndex (DatetimeIndexOpsMixin , Int64Index , DatetimeDelegateMixin ):
112106 """
113107 Immutable ndarray of datetime64 data, represented internally as int64, and
114108 which can be boxed to Timestamp objects that are subclasses of datetime and
@@ -268,6 +262,7 @@ def _join_i8_wrapper(joinf, **kwargs):
268262 _object_ops = DatetimeArray ._object_ops
269263 _field_ops = DatetimeArray ._field_ops
270264 _datetimelike_ops = DatetimeArray ._datetimelike_ops
265+ _datetimelike_methods = DatetimeArray ._datetimelike_methods
271266
272267 # --------------------------------------------------------------------
273268 # Constructors
@@ -294,8 +289,8 @@ def __new__(cls, data=None,
294289 "endpoints is deprecated. Use "
295290 "`pandas.date_range` instead." ,
296291 FutureWarning , stacklevel = 2 )
297-
298- return cls ( dtarr , name = name )
292+ return cls . _simple_new (
293+ dtarr . _data , freq = dtarr . freq , tz = dtarr . tz , name = name )
299294
300295 if is_scalar (data ):
301296 raise TypeError ("{cls}() must be called with a "
@@ -331,7 +326,11 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
331326 # DatetimeArray._simple_new will accept either i8 or M8[ns] dtypes
332327 assert isinstance (values , np .ndarray ), type (values )
333328
334- result = super (DatetimeIndex , cls )._simple_new (values , freq , tz )
329+ dtarr = DatetimeArray ._simple_new (values , freq = freq , tz = tz )
330+ result = object .__new__ (cls )
331+ result ._data = dtarr ._data
332+ result ._freq = dtarr .freq
333+ result ._tz = dtarr .tz
335334 result .name = name
336335 # For groupby perf. See note in indexes/base about _index_data
337336 result ._index_data = result ._data
@@ -340,6 +339,10 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
340339
341340 # --------------------------------------------------------------------
342341
342+ @property
343+ def dtype (self ):
344+ return self ._eadata .dtype
345+
343346 @property
344347 def _values (self ):
345348 # tz-naive -> ndarray
@@ -360,6 +363,8 @@ def tz(self, value):
360363 raise AttributeError ("Cannot directly set timezone. Use tz_localize() "
361364 "or tz_convert() as appropriate" )
362365
366+ tzinfo = tz
367+
363368 @property
364369 def size (self ):
365370 # TODO: Remove this when we have a DatetimeTZArray
@@ -670,7 +675,7 @@ def intersection(self, other):
670675 def _get_time_micros (self ):
671676 values = self .asi8
672677 if self .tz is not None and not timezones .is_utc (self .tz ):
673- values = self ._local_timestamps ()
678+ values = self ._eadata . _local_timestamps ()
674679 return fields .get_time_micros (values )
675680
676681 def to_series (self , keep_tz = None , index = None , name = None ):
@@ -1139,12 +1144,64 @@ def _eadata(self):
11391144 _is_monotonic_increasing = Index .is_monotonic_increasing
11401145 _is_monotonic_decreasing = Index .is_monotonic_decreasing
11411146 _is_unique = Index .is_unique
1142- astype = DatetimeIndexOpsMixin .astype
11431147
11441148 _timezone = cache_readonly (DatetimeArray ._timezone .fget )
11451149 is_normalized = cache_readonly (DatetimeArray .is_normalized .fget )
11461150 _resolution = cache_readonly (DatetimeArray ._resolution .fget )
11471151
1152+ strftime = ea_passthrough ("strftime" )
1153+ _has_same_tz = ea_passthrough ("_has_same_tz" )
1154+ __array__ = ea_passthrough ("__array__" )
1155+
1156+ @property
1157+ def offset (self ):
1158+ """
1159+ get/set the frequency of the instance
1160+ """
1161+ msg = ('{cls}.offset has been deprecated and will be removed '
1162+ 'in a future version; use {cls}.freq instead.'
1163+ .format (cls = type (self ).__name__ ))
1164+ warnings .warn (msg , FutureWarning , stacklevel = 2 )
1165+ return self .freq
1166+
1167+ @offset .setter
1168+ def offset (self , value ):
1169+ """
1170+ get/set the frequency of the instance
1171+ """
1172+ msg = ('{cls}.offset has been deprecated and will be removed '
1173+ 'in a future version; use {cls}.freq instead.'
1174+ .format (cls = type (self ).__name__ ))
1175+ warnings .warn (msg , FutureWarning , stacklevel = 2 )
1176+ self .freq = value
1177+
1178+ @property
1179+ def freq (self ):
1180+ return self ._freq
1181+
1182+ @freq .setter
1183+ def freq (self , value ):
1184+ if value is not None :
1185+ # let DatetimeArray to validation
1186+ self ._eadata .freq = value
1187+
1188+ self ._freq = to_offset (value )
1189+
1190+ def __getitem__ (self , key ):
1191+ result = self ._eadata .__getitem__ (key )
1192+ if is_scalar (result ):
1193+ return result
1194+ elif result .ndim > 1 :
1195+ # To support MPL which performs slicing with 2 dim
1196+ # even though it only has 1 dim by definition
1197+ assert isinstance (result , np .ndarray ), result
1198+ return result
1199+ return type (self )(result , name = self .name )
1200+
1201+ @property
1202+ def _box_func (self ):
1203+ return lambda x : Timestamp (x , tz = self .tz )
1204+
11481205 # --------------------------------------------------------------------
11491206
11501207 @Substitution (klass = 'DatetimeIndex' )
@@ -1486,9 +1543,8 @@ def date_range(start=None, end=None, periods=None, freq=None, tz=None,
14861543 start = start , end = end , periods = periods ,
14871544 freq = freq , tz = tz , normalize = normalize ,
14881545 closed = closed , ** kwargs )
1489-
1490- result = DatetimeIndex (dtarr , name = name )
1491- return result
1546+ return DatetimeIndex ._simple_new (
1547+ dtarr ._data , tz = dtarr .tz , freq = dtarr .freq , name = name )
14921548
14931549
14941550def bdate_range (start = None , end = None , periods = None , freq = 'B' , tz = None ,
0 commit comments