@@ -450,12 +450,12 @@ cpdef array_to_datetime(
450
450
Returns
451
451
-------
452
452
np.ndarray
453
- May be datetime64[ns ] or object dtype
453
+ May be datetime64[creso_unit ] or object dtype
454
454
tzinfo or None
455
455
"""
456
456
cdef:
457
457
Py_ssize_t i, n = values.size
458
- object val, tz
458
+ object val
459
459
ndarray[int64_t] iresult
460
460
npy_datetimestruct dts
461
461
bint utc_convert = bool (utc)
@@ -467,7 +467,7 @@ cpdef array_to_datetime(
467
467
_TSObject _ts
468
468
float tz_offset
469
469
set out_tzoffset_vals = set ()
470
- tzinfo tz_out = None
470
+ tzinfo tz, tz_out = None
471
471
cnp.flatiter it = cnp.PyArray_IterNew(values)
472
472
NPY_DATETIMEUNIT item_reso
473
473
bint infer_reso = creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC
@@ -522,15 +522,14 @@ cpdef array_to_datetime(
522
522
523
523
elif is_integer_object(val) or is_float_object(val):
524
524
# these must be ns unit by-definition
525
- item_reso = NPY_FR_ns
526
- state.update_creso(item_reso)
527
- if infer_reso:
528
- creso = state.creso
529
525
530
526
if val != val or val == NPY_NAT:
531
527
iresult[i] = NPY_NAT
532
528
else :
533
- # we now need to parse this as if unit='ns'
529
+ item_reso = NPY_FR_ns
530
+ state.update_creso(item_reso)
531
+ if infer_reso:
532
+ creso = state.creso
534
533
iresult[i] = cast_from_unit(val, " ns" , out_reso = creso)
535
534
state.found_other = True
536
535
@@ -552,6 +551,16 @@ cpdef array_to_datetime(
552
551
_ts = convert_str_to_tsobject(
553
552
val, None , dayfirst = dayfirst, yearfirst = yearfirst
554
553
)
554
+
555
+ if _ts.value == NPY_NAT:
556
+ # e.g. "NaT" string or empty string, we do not consider
557
+ # this as either tzaware or tznaive. See
558
+ # test_to_datetime_with_empty_str_utc_false_format_mixed
559
+ # We also do not update resolution inference based on this,
560
+ # see test_infer_with_nat_int_float_str
561
+ iresult[i] = _ts.value
562
+ continue
563
+
555
564
item_reso = _ts.creso
556
565
state.update_creso(item_reso)
557
566
if infer_reso:
@@ -562,12 +571,7 @@ cpdef array_to_datetime(
562
571
iresult[i] = _ts.value
563
572
564
573
tz = _ts.tzinfo
565
- if _ts.value == NPY_NAT:
566
- # e.g. "NaT" string or empty string, we do not consider
567
- # this as either tzaware or tznaive. See
568
- # test_to_datetime_with_empty_str_utc_false_format_mixed
569
- pass
570
- elif tz is not None :
574
+ if tz is not None :
571
575
# dateutil timezone objects cannot be hashed, so
572
576
# store the UTC offsets in seconds instead
573
577
nsecs = tz.utcoffset(None ).total_seconds()
@@ -640,7 +644,7 @@ cpdef array_to_datetime(
640
644
# Otherwise we can use the single reso that we encountered and avoid
641
645
# a second pass.
642
646
abbrev = npy_unit_to_abbrev(state.creso)
643
- result = iresult.view(f" M8[{abbrev}]" )
647
+ result = iresult.view(f" M8[{abbrev}]" ).reshape(result.shape)
644
648
return result, tz_out
645
649
646
650
0 commit comments