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,7 +289,7 @@ 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
295
def _return_parsed_timezone_results (result : np .ndarray , timezones , tz , name ) -> Index :
@@ -326,7 +325,7 @@ def _convert_listlike_datetimes(
326
325
arg ,
327
326
format : str | None ,
328
327
name : Hashable = None ,
329
- tz : Timezone | None = None ,
328
+ utc : bool = False ,
330
329
unit : str | None = None ,
331
330
errors : DateTimeErrorChoices = "raise" ,
332
331
infer_datetime_format : bool = False ,
@@ -344,8 +343,8 @@ def _convert_listlike_datetimes(
344
343
date to be parsed
345
344
name : object
346
345
None or string for the Index name
347
- tz : object
348
- None or 'utc'
346
+ utc : bool
347
+ Whether to convert/localize timestamps to UTC.
349
348
unit : str
350
349
None or string of the frequency of the passed data
351
350
errors : str
@@ -368,10 +367,11 @@ def _convert_listlike_datetimes(
368
367
369
368
arg_dtype = getattr (arg , "dtype" , None )
370
369
# these are shortcutable
370
+ tz = "utc" if utc else None
371
371
if is_datetime64tz_dtype (arg_dtype ):
372
372
if not isinstance (arg , (DatetimeArray , DatetimeIndex )):
373
373
return DatetimeIndex (arg , tz = tz , name = name )
374
- if tz == " utc" :
374
+ if utc :
375
375
arg = arg .tz_convert (None ).tz_localize (tz )
376
376
return arg
377
377
@@ -432,7 +432,6 @@ def _convert_listlike_datetimes(
432
432
if res is not None :
433
433
return res
434
434
435
- utc = tz == "utc"
436
435
result , tz_parsed = objects_to_datetime64ns (
437
436
arg ,
438
437
dayfirst = dayfirst ,
@@ -451,7 +450,6 @@ def _convert_listlike_datetimes(
451
450
dta = DatetimeArray (result , dtype = tz_to_dtype (tz_parsed ))
452
451
return DatetimeIndex ._simple_new (dta , name = name )
453
452
454
- utc = tz == "utc"
455
453
return _box_as_indexlike (result , utc = utc , name = name )
456
454
457
455
@@ -657,7 +655,7 @@ def to_datetime(
657
655
errors : DateTimeErrorChoices = ...,
658
656
dayfirst : bool = ...,
659
657
yearfirst : bool = ...,
660
- utc : bool | None = ...,
658
+ utc : bool = ...,
661
659
format : str | None = ...,
662
660
exact : bool = ...,
663
661
unit : str | None = ...,
@@ -674,7 +672,7 @@ def to_datetime(
674
672
errors : DateTimeErrorChoices = ...,
675
673
dayfirst : bool = ...,
676
674
yearfirst : bool = ...,
677
- utc : bool | None = ...,
675
+ utc : bool = ...,
678
676
format : str | None = ...,
679
677
exact : bool = ...,
680
678
unit : str | None = ...,
@@ -691,7 +689,7 @@ def to_datetime(
691
689
errors : DateTimeErrorChoices = ...,
692
690
dayfirst : bool = ...,
693
691
yearfirst : bool = ...,
694
- utc : bool | None = ...,
692
+ utc : bool = ...,
695
693
format : str | None = ...,
696
694
exact : bool = ...,
697
695
unit : str | None = ...,
@@ -707,7 +705,7 @@ def to_datetime(
707
705
errors : DateTimeErrorChoices = "raise" ,
708
706
dayfirst : bool = False ,
709
707
yearfirst : bool = False ,
710
- utc : bool | None = None ,
708
+ utc : bool = False ,
711
709
format : str | None = None ,
712
710
exact : bool = True ,
713
711
unit : str | None = None ,
@@ -756,7 +754,7 @@ def to_datetime(
756
754
``yearfirst=True`` is not strict, but will prefer to parse
757
755
with year first.
758
756
759
- utc : bool, default None
757
+ utc : bool, default False
760
758
Control timezone-related parsing, localization and conversion.
761
759
762
760
- If :const:`True`, the function *always* returns a timezone-aware
@@ -1049,10 +1047,9 @@ def to_datetime(
1049
1047
if origin != "unix" :
1050
1048
arg = _adjust_to_origin (arg , origin , unit )
1051
1049
1052
- tz = "utc" if utc else None
1053
1050
convert_listlike = partial (
1054
1051
_convert_listlike_datetimes ,
1055
- tz = tz ,
1052
+ utc = utc ,
1056
1053
unit = unit ,
1057
1054
dayfirst = dayfirst ,
1058
1055
yearfirst = yearfirst ,
@@ -1065,11 +1062,11 @@ def to_datetime(
1065
1062
1066
1063
if isinstance (arg , Timestamp ):
1067
1064
result = arg
1068
- if tz is not None :
1065
+ if utc :
1069
1066
if arg .tz is not None :
1070
- result = arg .tz_convert (tz )
1067
+ result = arg .tz_convert ("utc" )
1071
1068
else :
1072
- result = arg .tz_localize (tz )
1069
+ result = arg .tz_localize ("utc" )
1073
1070
elif isinstance (arg , ABCSeries ):
1074
1071
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
1075
1072
if not cache_array .empty :
@@ -1078,7 +1075,7 @@ def to_datetime(
1078
1075
values = convert_listlike (arg ._values , format )
1079
1076
result = arg ._constructor (values , index = arg .index , name = arg .name )
1080
1077
elif isinstance (arg , (ABCDataFrame , abc .MutableMapping )):
1081
- result = _assemble_from_unit_mappings (arg , errors , tz )
1078
+ result = _assemble_from_unit_mappings (arg , errors , utc )
1082
1079
elif isinstance (arg , Index ):
1083
1080
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
1084
1081
if not cache_array .empty :
@@ -1145,7 +1142,7 @@ def to_datetime(
1145
1142
}
1146
1143
1147
1144
1148
- def _assemble_from_unit_mappings (arg , errors : DateTimeErrorChoices , tz ):
1145
+ def _assemble_from_unit_mappings (arg , errors : DateTimeErrorChoices , utc : bool ):
1149
1146
"""
1150
1147
assemble the unit specified fields from the arg (DataFrame)
1151
1148
Return a Series for actual parsing
@@ -1158,7 +1155,8 @@ def _assemble_from_unit_mappings(arg, errors: DateTimeErrorChoices, tz):
1158
1155
- If :const:`'raise'`, then invalid parsing will raise an exception
1159
1156
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`
1160
1157
- If :const:`'ignore'`, then invalid parsing will return the input
1161
- tz : None or 'utc'
1158
+ utc : bool
1159
+ Whether to convert/localize timestamps to UTC.
1162
1160
1163
1161
Returns
1164
1162
-------
@@ -1221,7 +1219,7 @@ def coerce(values):
1221
1219
+ coerce (arg [unit_rev ["day" ]])
1222
1220
)
1223
1221
try :
1224
- values = to_datetime (values , format = "%Y%m%d" , errors = errors , utc = tz )
1222
+ values = to_datetime (values , format = "%Y%m%d" , errors = errors , utc = utc )
1225
1223
except (TypeError , ValueError ) as err :
1226
1224
raise ValueError (f"cannot assemble the datetimes: { err } " ) from err
1227
1225
0 commit comments