@@ -450,12 +450,12 @@ cpdef array_to_datetime(
450450 Returns
451451 -------
452452 np.ndarray
453- May be datetime64[ns ] or object dtype
453+ May be datetime64[creso_unit ] or object dtype
454454 tzinfo or None
455455 """
456456 cdef:
457457 Py_ssize_t i, n = values.size
458- object val, tz
458+ object val
459459 ndarray[int64_t] iresult
460460 npy_datetimestruct dts
461461 bint utc_convert = bool (utc)
@@ -467,7 +467,7 @@ cpdef array_to_datetime(
467467 _TSObject _ts
468468 float tz_offset
469469 set out_tzoffset_vals = set ()
470- tzinfo tz_out = None
470+ tzinfo tz, tz_out = None
471471 cnp.flatiter it = cnp.PyArray_IterNew(values)
472472 NPY_DATETIMEUNIT item_reso
473473 bint infer_reso = creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC
@@ -522,15 +522,14 @@ cpdef array_to_datetime(
522522
523523 elif is_integer_object(val) or is_float_object(val):
524524 # 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
529525
530526 if val != val or val == NPY_NAT:
531527 iresult[i] = NPY_NAT
532528 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
534533 iresult[i] = cast_from_unit(val, " ns" , out_reso = creso)
535534 state.found_other = True
536535
@@ -552,6 +551,16 @@ cpdef array_to_datetime(
552551 _ts = convert_str_to_tsobject(
553552 val, None , dayfirst = dayfirst, yearfirst = yearfirst
554553 )
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+
555564 item_reso = _ts.creso
556565 state.update_creso(item_reso)
557566 if infer_reso:
@@ -562,12 +571,7 @@ cpdef array_to_datetime(
562571 iresult[i] = _ts.value
563572
564573 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 :
571575 # dateutil timezone objects cannot be hashed, so
572576 # store the UTC offsets in seconds instead
573577 nsecs = tz.utcoffset(None ).total_seconds()
@@ -640,7 +644,7 @@ cpdef array_to_datetime(
640644 # Otherwise we can use the single reso that we encountered and avoid
641645 # a second pass.
642646 abbrev = npy_unit_to_abbrev(state.creso)
643- result = iresult.view(f" M8[{abbrev}]" )
647+ result = iresult.view(f" M8[{abbrev}]" ).reshape(result.shape)
644648 return result, tz_out
645649
646650
0 commit comments