12
12
from pandas .core .dtypes .common import (
13
13
_INT64_DTYPE ,
14
14
_NS_DTYPE ,
15
- is_object_dtype ,
16
15
is_datetime64_dtype ,
17
16
is_datetimetz ,
18
17
is_dtype_equal ,
@@ -551,10 +550,11 @@ def _generate(cls, start, end, periods, name, freq,
551
550
index = _generate_regular_range (start , end , periods , freq )
552
551
553
552
if tz is not None and getattr (index , 'tz' , None ) is None :
554
- index = conversion .tz_localize_to_utc (_ensure_int64 (index ),
555
- tz ,
556
- ambiguous = ambiguous )
557
- index = index .view (_NS_DTYPE )
553
+ arr = conversion .tz_localize_to_utc (_ensure_int64 (index ),
554
+ tz ,
555
+ ambiguous = ambiguous )
556
+
557
+ index = DatetimeIndex (arr )
558
558
559
559
# index is localized datetime64 array -> have to convert
560
560
# start/end as well to compare
@@ -575,7 +575,9 @@ def _generate(cls, start, end, periods, name, freq,
575
575
index = index [1 :]
576
576
if not right_closed and len (index ) and index [- 1 ] == end :
577
577
index = index [:- 1 ]
578
- index = cls ._simple_new (index , name = name , freq = freq , tz = tz )
578
+
579
+ index = cls ._simple_new (index .values , name = name , freq = freq , tz = tz )
580
+
579
581
return index
580
582
581
583
def _convert_for_op (self , value ):
@@ -606,12 +608,14 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
606
608
dtype = dtype , ** kwargs )
607
609
values = np .array (values , copy = False )
608
610
609
- if is_object_dtype (values ):
610
- return cls (values , name = name , freq = freq , tz = tz ,
611
- dtype = dtype , ** kwargs ).values
612
- elif not is_datetime64_dtype (values ):
611
+ if not is_datetime64_dtype (values ):
613
612
values = _ensure_int64 (values ).view (_NS_DTYPE )
614
613
614
+ values = getattr (values , 'values' , values )
615
+
616
+ assert isinstance (values , np .ndarray ), "values is not an np.ndarray"
617
+ assert is_datetime64_dtype (values )
618
+
615
619
result = super (DatetimeIndex , cls )._simple_new (values , freq , tz ,
616
620
** kwargs )
617
621
result .name = name
@@ -1000,7 +1004,7 @@ def unique(self, level=None):
1000
1004
else :
1001
1005
naive = self
1002
1006
result = super (DatetimeIndex , naive ).unique (level = level )
1003
- return self ._simple_new (result , name = self .name , tz = self .tz ,
1007
+ return self ._simple_new (result . values , name = self .name , tz = self .tz ,
1004
1008
freq = self .freq )
1005
1009
1006
1010
def union (self , other ):
@@ -1855,7 +1859,7 @@ def _generate_regular_range(start, end, periods, freq):
1855
1859
"if a 'period' is given." )
1856
1860
1857
1861
data = np .arange (b , e , stride , dtype = np .int64 )
1858
- data = DatetimeIndex ._simple_new (data , None , tz = tz )
1862
+ data = DatetimeIndex ._simple_new (data . view ( _NS_DTYPE ) , None , tz = tz )
1859
1863
else :
1860
1864
if isinstance (start , Timestamp ):
1861
1865
start = start .to_pydatetime ()
0 commit comments