From d3120f7a5d16892599ba96962d9a72f1da00c6fd Mon Sep 17 00:00:00 2001 From: Andreas Schwab Date: Wed, 21 Sep 2022 11:05:00 +0200 Subject: [PATCH 1/2] Handle NaN in array_with_unit_to_datetime Missing values in arrays can either be NaT (for integer types) or NaN (for floating types). Make sure to also handle the latter. This fixes all failing tests. --- pandas/_libs/tslib.pyx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 6f58fecd1ac81..054208fc147db 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -298,6 +298,10 @@ def array_with_unit_to_datetime( iresult = values.astype("i8", copy=False) # fill missing values by comparing to NPY_NAT mask = iresult == NPY_NAT + # Trying to Convert NaN to integer results in undefined + # behaviour, so handle it explicitly + if values.dtype.kind == "f": + mask |= values != values iresult[mask] = 0 fvalues = iresult.astype("f8") * mult need_to_iterate = False From c4c8d20314345265e5b85ea42e7c1d2952244159 Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 13 Dec 2022 13:22:35 +0000 Subject: [PATCH 2/2] Update pandas/_libs/tslib.pyx --- pandas/_libs/tslib.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 5c5e14dcc4ff6..6f0ab6eb0d532 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -301,7 +301,7 @@ def array_with_unit_to_datetime( # fill missing values by comparing to NPY_NAT mask = iresult == NPY_NAT # Trying to Convert NaN to integer results in undefined - # behaviour, so handle it explicitly + # behaviour, so handle it explicitly (see GH #48705) if values.dtype.kind == "f": mask |= values != values iresult[mask] = 0