38
38
AnyArrayLike ,
39
39
ArrayLike ,
40
40
DateTimeErrorChoices ,
41
- Timezone ,
42
41
npt ,
43
42
)
44
43
@@ -239,7 +238,7 @@ def _maybe_cache(
239
238
240
239
241
240
def _box_as_indexlike (
242
- dt_array : ArrayLike , utc : bool | None = None , name : Hashable = None
241
+ dt_array : ArrayLike , utc : bool = False , name : Hashable = None
243
242
) -> Index :
244
243
"""
245
244
Properly boxes the ndarray of datetimes to DatetimeIndex
@@ -249,8 +248,8 @@ def _box_as_indexlike(
249
248
----------
250
249
dt_array: 1-d array
251
250
Array of datetimes to be wrapped in an Index.
252
- tz : object
253
- None or 'utc'
251
+ utc : bool
252
+ Whether to convert/localize timestamps to UTC.
254
253
name : string, default None
255
254
Name for a resulting index
256
255
@@ -290,10 +289,12 @@ def _convert_and_box_cache(
290
289
from pandas import Series
291
290
292
291
result = Series (arg ).map (cache_array )
293
- return _box_as_indexlike (result ._values , utc = None , name = name )
292
+ return _box_as_indexlike (result ._values , utc = False , name = name )
294
293
295
294
296
- def _return_parsed_timezone_results (result : np .ndarray , timezones , tz , name ) -> Index :
295
+ def _return_parsed_timezone_results (
296
+ result : np .ndarray , timezones , utc : bool , name
297
+ ) -> Index :
297
298
"""
298
299
Return results from array_strptime if a %z or %Z directive was passed.
299
300
@@ -303,8 +304,8 @@ def _return_parsed_timezone_results(result: np.ndarray, timezones, tz, name) ->
303
304
int64 date representations of the dates
304
305
timezones : ndarray
305
306
pytz timezone objects
306
- tz : object
307
- None or pytz timezone object
307
+ utc : bool
308
+ Whether to convert/localize timestamps to UTC.
308
309
name : string, default None
309
310
Name for a DatetimeIndex
310
311
@@ -315,9 +316,9 @@ def _return_parsed_timezone_results(result: np.ndarray, timezones, tz, name) ->
315
316
tz_results = np .array (
316
317
[Timestamp (res ).tz_localize (zone ) for res , zone in zip (result , timezones )]
317
318
)
318
- if tz is not None :
319
+ if utc :
319
320
# Convert to the same tz
320
- tz_results = np .array ([tz_result .tz_convert (tz ) for tz_result in tz_results ])
321
+ tz_results = np .array ([tz_result .tz_convert ("utc" ) for tz_result in tz_results ])
321
322
322
323
return Index (tz_results , name = name )
323
324
@@ -326,7 +327,7 @@ def _convert_listlike_datetimes(
326
327
arg ,
327
328
format : str | None ,
328
329
name : Hashable = None ,
329
- tz : Timezone | None = None ,
330
+ utc : bool = False ,
330
331
unit : str | None = None ,
331
332
errors : DateTimeErrorChoices = "raise" ,
332
333
infer_datetime_format : bool = False ,
@@ -344,8 +345,8 @@ def _convert_listlike_datetimes(
344
345
date to be parsed
345
346
name : object
346
347
None or string for the Index name
347
- tz : object
348
- None or 'utc'
348
+ utc : bool
349
+ Whether to convert/localize timestamps to UTC.
349
350
unit : str
350
351
None or string of the frequency of the passed data
351
352
errors : str
@@ -368,11 +369,12 @@ def _convert_listlike_datetimes(
368
369
369
370
arg_dtype = getattr (arg , "dtype" , None )
370
371
# these are shortcutable
372
+ tz = "utc" if utc else None
371
373
if is_datetime64tz_dtype (arg_dtype ):
372
374
if not isinstance (arg , (DatetimeArray , DatetimeIndex )):
373
375
return DatetimeIndex (arg , tz = tz , name = name )
374
- if tz == " utc" :
375
- arg = arg .tz_convert (None ).tz_localize (tz )
376
+ if utc :
377
+ arg = arg .tz_convert (None ).tz_localize ("utc" )
376
378
return arg
377
379
378
380
elif is_datetime64_ns_dtype (arg_dtype ):
@@ -381,16 +383,16 @@ def _convert_listlike_datetimes(
381
383
return DatetimeIndex (arg , tz = tz , name = name )
382
384
except ValueError :
383
385
pass
384
- elif tz :
386
+ elif utc :
385
387
# DatetimeArray, DatetimeIndex
386
- return arg .tz_localize (tz )
388
+ return arg .tz_localize ("utc" )
387
389
388
390
return arg
389
391
390
392
elif unit is not None :
391
393
if format is not None :
392
394
raise ValueError ("cannot specify both format and unit" )
393
- return _to_datetime_with_unit (arg , unit , name , tz , errors )
395
+ return _to_datetime_with_unit (arg , unit , name , utc , errors )
394
396
elif getattr (arg , "ndim" , 1 ) > 1 :
395
397
raise TypeError (
396
398
"arg must be a string, datetime, list, tuple, 1-d array, or Series"
@@ -427,12 +429,11 @@ def _convert_listlike_datetimes(
427
429
428
430
if format is not None and not require_iso8601 :
429
431
res = _to_datetime_with_format (
430
- arg , orig_arg , name , tz , format , exact , errors , infer_datetime_format
432
+ arg , orig_arg , name , utc , format , exact , errors , infer_datetime_format
431
433
)
432
434
if res is not None :
433
435
return res
434
436
435
- utc = tz == "utc"
436
437
result , tz_parsed = objects_to_datetime64ns (
437
438
arg ,
438
439
dayfirst = dayfirst ,
@@ -451,14 +452,13 @@ def _convert_listlike_datetimes(
451
452
dta = DatetimeArray (result , dtype = tz_to_dtype (tz_parsed ))
452
453
return DatetimeIndex ._simple_new (dta , name = name )
453
454
454
- utc = tz == "utc"
455
455
return _box_as_indexlike (result , utc = utc , name = name )
456
456
457
457
458
458
def _array_strptime_with_fallback (
459
459
arg ,
460
460
name ,
461
- tz ,
461
+ utc : bool ,
462
462
fmt : str ,
463
463
exact : bool ,
464
464
errors : str ,
@@ -467,8 +467,6 @@ def _array_strptime_with_fallback(
467
467
"""
468
468
Call array_strptime, with fallback behavior depending on 'errors'.
469
469
"""
470
- utc = tz == "utc"
471
-
472
470
try :
473
471
result , timezones = array_strptime (arg , fmt , exact = exact , errors = errors )
474
472
except OutOfBoundsDatetime :
@@ -498,7 +496,7 @@ def _array_strptime_with_fallback(
498
496
return None
499
497
else :
500
498
if "%Z" in fmt or "%z" in fmt :
501
- return _return_parsed_timezone_results (result , timezones , tz , name )
499
+ return _return_parsed_timezone_results (result , timezones , utc , name )
502
500
503
501
return _box_as_indexlike (result , utc = utc , name = name )
504
502
@@ -507,7 +505,7 @@ def _to_datetime_with_format(
507
505
arg ,
508
506
orig_arg ,
509
507
name ,
510
- tz ,
508
+ utc : bool ,
511
509
fmt : str ,
512
510
exact : bool ,
513
511
errors : str ,
@@ -531,17 +529,16 @@ def _to_datetime_with_format(
531
529
"cannot convert the input to '%Y%m%d' date format"
532
530
) from err
533
531
if result is not None :
534
- utc = tz == "utc"
535
532
return _box_as_indexlike (result , utc = utc , name = name )
536
533
537
534
# fallback
538
535
res = _array_strptime_with_fallback (
539
- arg , name , tz , fmt , exact , errors , infer_datetime_format
536
+ arg , name , utc , fmt , exact , errors , infer_datetime_format
540
537
)
541
538
return res
542
539
543
540
544
- def _to_datetime_with_unit (arg , unit , name , tz , errors : str ) -> Index :
541
+ def _to_datetime_with_unit (arg , unit , name , utc : bool , errors : str ) -> Index :
545
542
"""
546
543
to_datetime specalized to the case where a 'unit' is passed.
547
544
"""
@@ -570,11 +567,11 @@ def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index:
570
567
# result will be naive but in UTC
571
568
result = result .tz_localize ("UTC" ).tz_convert (tz_parsed )
572
569
573
- if tz is not None :
570
+ if utc :
574
571
if result .tz is None :
575
- result = result .tz_localize (tz )
572
+ result = result .tz_localize ("utc" )
576
573
else :
577
- result = result .tz_convert (tz )
574
+ result = result .tz_convert ("utc" )
578
575
return result
579
576
580
577
@@ -657,7 +654,7 @@ def to_datetime(
657
654
errors : DateTimeErrorChoices = ...,
658
655
dayfirst : bool = ...,
659
656
yearfirst : bool = ...,
660
- utc : bool | None = ...,
657
+ utc : bool = ...,
661
658
format : str | None = ...,
662
659
exact : bool = ...,
663
660
unit : str | None = ...,
@@ -674,7 +671,7 @@ def to_datetime(
674
671
errors : DateTimeErrorChoices = ...,
675
672
dayfirst : bool = ...,
676
673
yearfirst : bool = ...,
677
- utc : bool | None = ...,
674
+ utc : bool = ...,
678
675
format : str | None = ...,
679
676
exact : bool = ...,
680
677
unit : str | None = ...,
@@ -691,7 +688,7 @@ def to_datetime(
691
688
errors : DateTimeErrorChoices = ...,
692
689
dayfirst : bool = ...,
693
690
yearfirst : bool = ...,
694
- utc : bool | None = ...,
691
+ utc : bool = ...,
695
692
format : str | None = ...,
696
693
exact : bool = ...,
697
694
unit : str | None = ...,
@@ -707,7 +704,7 @@ def to_datetime(
707
704
errors : DateTimeErrorChoices = "raise" ,
708
705
dayfirst : bool = False ,
709
706
yearfirst : bool = False ,
710
- utc : bool | None = None ,
707
+ utc : bool = False ,
711
708
format : str | None = None ,
712
709
exact : bool = True ,
713
710
unit : str | None = None ,
@@ -756,7 +753,7 @@ def to_datetime(
756
753
``yearfirst=True`` is not strict, but will prefer to parse
757
754
with year first.
758
755
759
- utc : bool, default None
756
+ utc : bool, default False
760
757
Control timezone-related parsing, localization and conversion.
761
758
762
759
- If :const:`True`, the function *always* returns a timezone-aware
@@ -1049,10 +1046,9 @@ def to_datetime(
1049
1046
if origin != "unix" :
1050
1047
arg = _adjust_to_origin (arg , origin , unit )
1051
1048
1052
- tz = "utc" if utc else None
1053
1049
convert_listlike = partial (
1054
1050
_convert_listlike_datetimes ,
1055
- tz = tz ,
1051
+ utc = utc ,
1056
1052
unit = unit ,
1057
1053
dayfirst = dayfirst ,
1058
1054
yearfirst = yearfirst ,
@@ -1065,11 +1061,11 @@ def to_datetime(
1065
1061
1066
1062
if isinstance (arg , Timestamp ):
1067
1063
result = arg
1068
- if tz is not None :
1064
+ if utc :
1069
1065
if arg .tz is not None :
1070
- result = arg .tz_convert (tz )
1066
+ result = arg .tz_convert ("utc" )
1071
1067
else :
1072
- result = arg .tz_localize (tz )
1068
+ result = arg .tz_localize ("utc" )
1073
1069
elif isinstance (arg , ABCSeries ):
1074
1070
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
1075
1071
if not cache_array .empty :
@@ -1078,7 +1074,7 @@ def to_datetime(
1078
1074
values = convert_listlike (arg ._values , format )
1079
1075
result = arg ._constructor (values , index = arg .index , name = arg .name )
1080
1076
elif isinstance (arg , (ABCDataFrame , abc .MutableMapping )):
1081
- result = _assemble_from_unit_mappings (arg , errors , tz )
1077
+ result = _assemble_from_unit_mappings (arg , errors , utc )
1082
1078
elif isinstance (arg , Index ):
1083
1079
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
1084
1080
if not cache_array .empty :
@@ -1145,7 +1141,7 @@ def to_datetime(
1145
1141
}
1146
1142
1147
1143
1148
- def _assemble_from_unit_mappings (arg , errors : DateTimeErrorChoices , tz ):
1144
+ def _assemble_from_unit_mappings (arg , errors : DateTimeErrorChoices , utc : bool ):
1149
1145
"""
1150
1146
assemble the unit specified fields from the arg (DataFrame)
1151
1147
Return a Series for actual parsing
@@ -1158,7 +1154,8 @@ def _assemble_from_unit_mappings(arg, errors: DateTimeErrorChoices, tz):
1158
1154
- If :const:`'raise'`, then invalid parsing will raise an exception
1159
1155
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`
1160
1156
- If :const:`'ignore'`, then invalid parsing will return the input
1161
- tz : None or 'utc'
1157
+ utc : bool
1158
+ Whether to convert/localize timestamps to UTC.
1162
1159
1163
1160
Returns
1164
1161
-------
@@ -1221,7 +1218,7 @@ def coerce(values):
1221
1218
+ coerce (arg [unit_rev ["day" ]])
1222
1219
)
1223
1220
try :
1224
- values = to_datetime (values , format = "%Y%m%d" , errors = errors , utc = tz )
1221
+ values = to_datetime (values , format = "%Y%m%d" , errors = errors , utc = utc )
1225
1222
except (TypeError , ValueError ) as err :
1226
1223
raise ValueError (f"cannot assemble the datetimes: { err } " ) from err
1227
1224
0 commit comments