1212from pandas .core .dtypes .common import (
1313 _INT64_DTYPE ,
1414 _NS_DTYPE ,
15- is_object_dtype ,
1615 is_datetime64_dtype ,
1716 is_datetimetz ,
1817 is_dtype_equal ,
@@ -551,10 +550,11 @@ def _generate(cls, start, end, periods, name, freq,
551550 index = _generate_regular_range (start , end , periods , freq )
552551
553552 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 )
558558
559559 # index is localized datetime64 array -> have to convert
560560 # start/end as well to compare
@@ -575,7 +575,9 @@ def _generate(cls, start, end, periods, name, freq,
575575 index = index [1 :]
576576 if not right_closed and len (index ) and index [- 1 ] == end :
577577 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+
579581 return index
580582
581583 def _convert_for_op (self , value ):
@@ -606,12 +608,14 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
606608 dtype = dtype , ** kwargs )
607609 values = np .array (values , copy = False )
608610
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 ):
613612 values = _ensure_int64 (values ).view (_NS_DTYPE )
614613
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+
615619 result = super (DatetimeIndex , cls )._simple_new (values , freq , tz ,
616620 ** kwargs )
617621 result .name = name
@@ -1000,7 +1004,7 @@ def unique(self, level=None):
10001004 else :
10011005 naive = self
10021006 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 ,
10041008 freq = self .freq )
10051009
10061010 def union (self , other ):
@@ -1855,7 +1859,7 @@ def _generate_regular_range(start, end, periods, freq):
18551859 "if a 'period' is given." )
18561860
18571861 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 )
18591863 else :
18601864 if isinstance (start , Timestamp ):
18611865 start = start .to_pydatetime ()
0 commit comments