diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index e30177a43f6b8..6efff2483e1ae 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2250,9 +2250,7 @@ def _sequence_to_dt64( ) return result, tz, None else: - # data comes back here as either i8 to denote UTC timestamps - # or M8[ns] to denote wall times - converted, inferred_tz = objects_to_datetime64ns( + converted, inferred_tz = objects_to_datetime64( data, dayfirst=dayfirst, yearfirst=yearfirst, @@ -2262,13 +2260,13 @@ def _sequence_to_dt64( copy = False if tz and inferred_tz: # two timezones: convert to intended from base UTC repr - assert converted.dtype == "i8" - # GH#42505 - # by convention, these are _already_ UTC, e.g + # GH#42505 by convention, these are _already_ UTC + assert converted.dtype == out_dtype, converted.dtype result = converted.view(out_dtype) elif inferred_tz: tz = inferred_tz + assert converted.dtype == out_dtype, converted.dtype result = converted.view(out_dtype) else: @@ -2360,7 +2358,7 @@ def _construct_from_dt64_naive( return result, copy -def objects_to_datetime64ns( +def objects_to_datetime64( data: np.ndarray, dayfirst, yearfirst, @@ -2388,10 +2386,11 @@ def objects_to_datetime64ns( Returns ------- result : ndarray - np.int64 dtype if returned values represent UTC timestamps - np.datetime64[ns] if returned values represent wall times + np.datetime64[out_unit] if returned values represent wall times or UTC + timestamps. object if mixed timezones inferred_tz : tzinfo or None + If not None, then the datetime64 values in `result` denote UTC timestamps. Raises ------ @@ -2414,11 +2413,8 @@ def objects_to_datetime64ns( if tz_parsed is not None: # We can take a shortcut since the datetime64 numpy array # is in UTC - # Return i8 values to denote unix timestamps - return result.view("i8"), tz_parsed + return result, tz_parsed elif result.dtype.kind == "M": - # returning M8[ns] denotes wall-times; since tz is None - # the distinction is a thin one return result, tz_parsed elif result.dtype == object: # GH#23675 when called via `pd.to_datetime`, returning an object-dtype diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 33ac5a169b08d..ea5e6e46f58ec 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -71,7 +71,7 @@ from pandas.core.arrays.base import ExtensionArray from pandas.core.arrays.datetimes import ( maybe_convert_dtype, - objects_to_datetime64ns, + objects_to_datetime64, tz_to_dtype, ) from pandas.core.construction import extract_array @@ -485,7 +485,7 @@ def _convert_listlike_datetimes( if format is not None and format != "mixed": return _array_strptime_with_fallback(arg, name, utc, format, exact, errors) - result, tz_parsed = objects_to_datetime64ns( + result, tz_parsed = objects_to_datetime64( arg, dayfirst=dayfirst, yearfirst=yearfirst, @@ -499,7 +499,7 @@ def _convert_listlike_datetimes( # is in UTC dtype = cast(DatetimeTZDtype, tz_to_dtype(tz_parsed)) dt64_values = result.view(f"M8[{dtype.unit}]") - dta = DatetimeArray(dt64_values, dtype=dtype) + dta = DatetimeArray._simple_new(dt64_values, dtype=dtype) return DatetimeIndex._simple_new(dta, name=name) return _box_as_indexlike(result, utc=utc, name=name)