3535_midnight = time (0 , 0 )
3636
3737
38+ def tz_to_dtype (tz ):
39+ """
40+ Return a datetime64[ns] dtype appropriate for the given timezone.
41+
42+ Parameters
43+ ----------
44+ tz : tzinfo or None
45+
46+ Returns
47+ -------
48+ np.dtype or Datetime64TZDType
49+ """
50+ if tz is None :
51+ return _NS_DTYPE
52+ else :
53+ return DatetimeTZDtype (tz = tz )
54+
55+
3856def _to_M8 (key , tz = None ):
3957 """
4058 Timestamp-like => dt64
@@ -305,13 +323,7 @@ def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False):
305323 self ._freq = freq
306324
307325 @classmethod
308- def _simple_new (cls , values , freq = None , tz = None ):
309- """
310- we require the we have a dtype compat for the values
311- if we are passed a non-dtype compat, then coerce using the constructor
312- """
313- dtype = DatetimeTZDtype (tz = tz ) if tz else _NS_DTYPE
314-
326+ def _simple_new (cls , values , freq = None , dtype = None ):
315327 return cls (values , freq = freq , dtype = dtype )
316328
317329 @classmethod
@@ -328,7 +340,8 @@ def _from_sequence(cls, data, dtype=None, copy=False,
328340 freq , freq_infer = dtl .validate_inferred_freq (freq , inferred_freq ,
329341 freq_infer )
330342
331- result = cls ._simple_new (subarr , freq = freq , tz = tz )
343+ dtype = tz_to_dtype (tz )
344+ result = cls ._simple_new (subarr , freq = freq , dtype = dtype )
332345
333346 if inferred_freq is None and freq is not None :
334347 # this condition precludes `freq_infer`
@@ -395,7 +408,7 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
395408 end = end .tz_localize (None )
396409 # TODO: consider re-implementing _cached_range; GH#17914
397410 values , _tz = generate_regular_range (start , end , periods , freq )
398- index = cls ._simple_new (values , freq = freq , tz = _tz )
411+ index = cls ._simple_new (values , freq = freq , dtype = tz_to_dtype ( _tz ) )
399412
400413 if tz is not None and index .tz is None :
401414 arr = conversion .tz_localize_to_utc (
@@ -418,16 +431,18 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
418431 arr = np .linspace (
419432 0 , end .value - start .value ,
420433 periods , dtype = 'int64' ) + start .value
434+ dtype = tz_to_dtype (tz )
421435 index = cls ._simple_new (
422- arr .astype ('M8[ns]' , copy = False ), freq = None , tz = tz
436+ arr .astype ('M8[ns]' , copy = False ), freq = None , dtype = dtype
423437 )
424438
425439 if not left_closed and len (index ) and index [0 ] == start :
426440 index = index [1 :]
427441 if not right_closed and len (index ) and index [- 1 ] == end :
428442 index = index [:- 1 ]
429443
430- return cls ._simple_new (index .asi8 , freq = freq , tz = tz )
444+ dtype = tz_to_dtype (tz )
445+ return cls ._simple_new (index .asi8 , freq = freq , dtype = dtype )
431446
432447 # -----------------------------------------------------------------
433448 # DatetimeLike Interface
@@ -806,7 +821,8 @@ def tz_convert(self, tz):
806821 'tz_localize to localize' )
807822
808823 # No conversion since timestamps are all UTC to begin with
809- return self ._simple_new (self .asi8 , tz = tz , freq = self .freq )
824+ dtype = tz_to_dtype (tz )
825+ return self ._simple_new (self .asi8 , dtype = dtype , freq = self .freq )
810826
811827 def tz_localize (self , tz , ambiguous = 'raise' , nonexistent = 'raise' ,
812828 errors = None ):
@@ -995,7 +1011,8 @@ def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
9951011 self .asi8 , tz , ambiguous = ambiguous , nonexistent = nonexistent ,
9961012 )
9971013 new_dates = new_dates .view (_NS_DTYPE )
998- return self ._simple_new (new_dates , tz = tz , freq = self .freq )
1014+ dtype = tz_to_dtype (tz )
1015+ return self ._simple_new (new_dates , dtype = dtype , freq = self .freq )
9991016
10001017 # ----------------------------------------------------------------
10011018 # Conversion Methods - Vectorized analogues of Timestamp methods
0 commit comments