26
26
import pandas .core .common as com
27
27
from pandas .core .indexes .base import Index
28
28
from pandas .core .indexes .datetimelike import (
29
- DatetimeIndexOpsMixin , DatetimelikeDelegateMixin )
29
+ DatetimeIndexOpsMixin , DatetimelikeDelegateMixin , ea_passthrough )
30
30
from pandas .core .indexes .numeric import Int64Index
31
31
from pandas .core .ops import get_op_result_name
32
32
import pandas .core .tools .datetimes as tools
@@ -96,19 +96,13 @@ class DatetimeDelegateMixin(DatetimelikeDelegateMixin):
96
96
_delegate_class = DatetimeArray
97
97
98
98
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 )
104
99
@delegate_names (DatetimeArray ,
105
100
DatetimeDelegateMixin ._delegated_properties ,
106
101
typ = "property" )
107
102
@delegate_names (DatetimeArray ,
108
103
DatetimeDelegateMixin ._delegated_methods ,
109
104
typ = "method" , overwrite = False )
110
- class DatetimeIndex (DatetimeArray , DatetimeIndexOpsMixin , Int64Index ,
111
- DatetimeDelegateMixin ):
105
+ class DatetimeIndex (DatetimeIndexOpsMixin , Int64Index , DatetimeDelegateMixin ):
112
106
"""
113
107
Immutable ndarray of datetime64 data, represented internally as int64, and
114
108
which can be boxed to Timestamp objects that are subclasses of datetime and
@@ -268,6 +262,7 @@ def _join_i8_wrapper(joinf, **kwargs):
268
262
_object_ops = DatetimeArray ._object_ops
269
263
_field_ops = DatetimeArray ._field_ops
270
264
_datetimelike_ops = DatetimeArray ._datetimelike_ops
265
+ _datetimelike_methods = DatetimeArray ._datetimelike_methods
271
266
272
267
# --------------------------------------------------------------------
273
268
# Constructors
@@ -294,8 +289,8 @@ def __new__(cls, data=None,
294
289
"endpoints is deprecated. Use "
295
290
"`pandas.date_range` instead." ,
296
291
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 )
299
294
300
295
if is_scalar (data ):
301
296
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):
331
326
# DatetimeArray._simple_new will accept either i8 or M8[ns] dtypes
332
327
assert isinstance (values , np .ndarray ), type (values )
333
328
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
335
334
result .name = name
336
335
# For groupby perf. See note in indexes/base about _index_data
337
336
result ._index_data = result ._data
@@ -340,6 +339,10 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
340
339
341
340
# --------------------------------------------------------------------
342
341
342
+ @property
343
+ def dtype (self ):
344
+ return self ._eadata .dtype
345
+
343
346
@property
344
347
def _values (self ):
345
348
# tz-naive -> ndarray
@@ -360,6 +363,8 @@ def tz(self, value):
360
363
raise AttributeError ("Cannot directly set timezone. Use tz_localize() "
361
364
"or tz_convert() as appropriate" )
362
365
366
+ tzinfo = tz
367
+
363
368
@property
364
369
def size (self ):
365
370
# TODO: Remove this when we have a DatetimeTZArray
@@ -670,7 +675,7 @@ def intersection(self, other):
670
675
def _get_time_micros (self ):
671
676
values = self .asi8
672
677
if self .tz is not None and not timezones .is_utc (self .tz ):
673
- values = self ._local_timestamps ()
678
+ values = self ._eadata . _local_timestamps ()
674
679
return fields .get_time_micros (values )
675
680
676
681
def to_series (self , keep_tz = None , index = None , name = None ):
@@ -1139,12 +1144,64 @@ def _eadata(self):
1139
1144
_is_monotonic_increasing = Index .is_monotonic_increasing
1140
1145
_is_monotonic_decreasing = Index .is_monotonic_decreasing
1141
1146
_is_unique = Index .is_unique
1142
- astype = DatetimeIndexOpsMixin .astype
1143
1147
1144
1148
_timezone = cache_readonly (DatetimeArray ._timezone .fget )
1145
1149
is_normalized = cache_readonly (DatetimeArray .is_normalized .fget )
1146
1150
_resolution = cache_readonly (DatetimeArray ._resolution .fget )
1147
1151
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
+
1148
1205
# --------------------------------------------------------------------
1149
1206
1150
1207
@Substitution (klass = 'DatetimeIndex' )
@@ -1486,9 +1543,8 @@ def date_range(start=None, end=None, periods=None, freq=None, tz=None,
1486
1543
start = start , end = end , periods = periods ,
1487
1544
freq = freq , tz = tz , normalize = normalize ,
1488
1545
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 )
1492
1548
1493
1549
1494
1550
def bdate_range (start = None , end = None , periods = None , freq = 'B' , tz = None ,
0 commit comments