35
35
_midnight = time (0 , 0 )
36
36
37
37
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
+
38
56
def _to_M8 (key , tz = None ):
39
57
"""
40
58
Timestamp-like => dt64
@@ -305,13 +323,7 @@ def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False):
305
323
self ._freq = freq
306
324
307
325
@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 ):
315
327
return cls (values , freq = freq , dtype = dtype )
316
328
317
329
@classmethod
@@ -328,7 +340,8 @@ def _from_sequence(cls, data, dtype=None, copy=False,
328
340
freq , freq_infer = dtl .validate_inferred_freq (freq , inferred_freq ,
329
341
freq_infer )
330
342
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 )
332
345
333
346
if inferred_freq is None and freq is not None :
334
347
# this condition precludes `freq_infer`
@@ -395,7 +408,7 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
395
408
end = end .tz_localize (None )
396
409
# TODO: consider re-implementing _cached_range; GH#17914
397
410
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 ) )
399
412
400
413
if tz is not None and index .tz is None :
401
414
arr = conversion .tz_localize_to_utc (
@@ -418,16 +431,18 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
418
431
arr = np .linspace (
419
432
0 , end .value - start .value ,
420
433
periods , dtype = 'int64' ) + start .value
434
+ dtype = tz_to_dtype (tz )
421
435
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
423
437
)
424
438
425
439
if not left_closed and len (index ) and index [0 ] == start :
426
440
index = index [1 :]
427
441
if not right_closed and len (index ) and index [- 1 ] == end :
428
442
index = index [:- 1 ]
429
443
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 )
431
446
432
447
# -----------------------------------------------------------------
433
448
# DatetimeLike Interface
@@ -806,7 +821,8 @@ def tz_convert(self, tz):
806
821
'tz_localize to localize' )
807
822
808
823
# 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 )
810
826
811
827
def tz_localize (self , tz , ambiguous = 'raise' , nonexistent = 'raise' ,
812
828
errors = None ):
@@ -995,7 +1011,8 @@ def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
995
1011
self .asi8 , tz , ambiguous = ambiguous , nonexistent = nonexistent ,
996
1012
)
997
1013
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 )
999
1016
1000
1017
# ----------------------------------------------------------------
1001
1018
# Conversion Methods - Vectorized analogues of Timestamp methods
0 commit comments