From 354b66741d0ea872f108c27010d3118d9af5a670 Mon Sep 17 00:00:00 2001 From: Roxen-AS Date: Sat, 28 Nov 2026 21:33:33 +0530 Subject: [PATCH 1/3] fix(db-dtypes): cast TimeArray to datetime64 instead of timedelta64 (#19190) Signed-off-by: Roxen-AS --- packages/db-dtypes/db_dtypes/__init__.py | 13 +++++++++++-- packages/db-dtypes/tests/unit/test_dtypes.py | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/db-dtypes/db_dtypes/__init__.py b/packages/db-dtypes/db_dtypes/__init__.py index 3943c2a0e285..680caae93c9d 100644 --- a/packages/db-dtypes/db_dtypes/__init__.py +++ b/packages/db-dtypes/db_dtypes/__init__.py @@ -168,11 +168,20 @@ def _box_func(self, x): ) # Integers are stored with nanosecond precision. return x.astype(_NP_BOX_DTYPE).item().time() - __return_deltas = {"timedelta", "timedelta64", "timedelta64[ns]", " Date: Sat, 28 Nov 2026 23:30:53 +0530 Subject: [PATCH 2/3] fix(db-dtypes): support endianness prefixes and generic M8 in astype datetime conversion Signed-off-by: Roxen-AS --- packages/db-dtypes/db_dtypes/__init__.py | 22 +++++++------------- packages/db-dtypes/tests/unit/test_dtypes.py | 9 +++++++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/db-dtypes/db_dtypes/__init__.py b/packages/db-dtypes/db_dtypes/__init__.py index 680caae93c9d..569de251e622 100644 --- a/packages/db-dtypes/db_dtypes/__init__.py +++ b/packages/db-dtypes/db_dtypes/__init__.py @@ -172,19 +172,16 @@ def _box_func(self, x): def astype(self, dtype, copy=True): stype = str(dtype) - if stype.startswith("datetime"): - if stype == "datetime" or stype == "datetime64": - dtype = self._ndarray.dtype - return self._ndarray.astype(dtype, copy=copy) - elif stype.startswith("=|") + if stype.startswith("datetime") or clean_stype.startswith("M8"): + if stype in ("datetime", "datetime64") or clean_stype == "M8": dtype = self._ndarray.dtype return self._ndarray.astype(dtype, copy=copy) deltas = self._ndarray - _NPEPOCH - if stype in self.__return_deltas: + if stype in self.__return_deltas or clean_stype in ("m8", "m8[ns]"): return deltas - elif stype.startswith("timedelta64[") or stype.startswith("=|") + if stype.startswith("datetime") or clean_stype.startswith("M8"): + if stype in ("datetime", "datetime64") or clean_stype == "M8": dtype = self._ndarray.dtype return self._ndarray.astype(dtype, copy=copy) diff --git a/packages/db-dtypes/tests/unit/test_dtypes.py b/packages/db-dtypes/tests/unit/test_dtypes.py index 7a63beed38fb..2b231cf1d92e 100644 --- a/packages/db-dtypes/tests/unit/test_dtypes.py +++ b/packages/db-dtypes/tests/unit/test_dtypes.py @@ -469,12 +469,17 @@ def test_astype_copy(dtype): [ (" Date: Thu, 3 Dec 2026 17:42:02 +0530 Subject: [PATCH 3/3] fix(db-dtypes): narrow TimeArray fix to --- packages/db-dtypes/db_dtypes/__init__.py | 22 +++++++---------- packages/db-dtypes/tests/unit/test_dtypes.py | 25 ++++++-------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/packages/db-dtypes/db_dtypes/__init__.py b/packages/db-dtypes/db_dtypes/__init__.py index 569de251e622..f51490ecacf3 100644 --- a/packages/db-dtypes/db_dtypes/__init__.py +++ b/packages/db-dtypes/db_dtypes/__init__.py @@ -34,7 +34,6 @@ time_dtype_name = "dbtime" _EPOCH = datetime.datetime(1970, 1, 1) _NPEPOCH = numpy.datetime64(_EPOCH, "ns") -_NP_DTYPE = "datetime64[ns]" # Numpy converts datetime64 scalars to datetime.datetime only if microsecond or # smaller precision is used. @@ -171,17 +170,11 @@ def _box_func(self, x): __return_deltas = {"timedelta", "timedelta64", "timedelta64[ns]", "=|") - if stype.startswith("datetime") or clean_stype.startswith("M8"): - if stype in ("datetime", "datetime64") or clean_stype == "M8": - dtype = self._ndarray.dtype - return self._ndarray.astype(dtype, copy=copy) - deltas = self._ndarray - _NPEPOCH - if stype in self.__return_deltas or clean_stype in ("m8", "m8[ns]"): + stype = str(dtype) + if stype in self.__return_deltas: return deltas - elif stype.startswith("timedelta64[") or clean_stype.startswith("m8["): + elif stype.startswith("timedelta64[") or stype.startswith("=|") - if stype.startswith("datetime") or clean_stype.startswith("M8"): - if stype in ("datetime", "datetime64") or clean_stype == "M8": + if stype.startswith("datetime"): + if stype == "datetime" or stype == "datetime64": + dtype = self._ndarray.dtype + return self._ndarray.astype(dtype, copy=copy) + elif stype.startswith("