From 34c0c423e9181270fcbf7f19aebf9ba81832c4fd Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 13 Nov 2023 14:29:09 -0800 Subject: [PATCH 1/2] REF: simplify objects_to_datetime64ns --- pandas/core/arrays/datetimes.py | 26 +++++++++++--------------- pandas/core/tools/datetimes.py | 6 +++--- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index e30177a43f6b8..f8ac632ca8991 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,10 +2358,10 @@ def _construct_from_dt64_naive( return result, copy -def objects_to_datetime64ns( +def objects_to_datetime64( data: np.ndarray, - dayfirst, - yearfirst, + dayfirst: bool, + yearfirst: bool, utc: bool = False, errors: DateTimeErrorChoices = "raise", allow_object: bool = False, @@ -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) From 5cab0c5e3fb761a3fdd38550d21de0b732761489 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 13 Nov 2023 15:15:56 -0800 Subject: [PATCH 2/2] revert annotations --- pandas/core/arrays/datetimes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index f8ac632ca8991..6efff2483e1ae 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2360,8 +2360,8 @@ def _construct_from_dt64_naive( def objects_to_datetime64( data: np.ndarray, - dayfirst: bool, - yearfirst: bool, + dayfirst, + yearfirst, utc: bool = False, errors: DateTimeErrorChoices = "raise", allow_object: bool = False,