Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix NaT handling in the PyArray_CompareFunc for datetime and timedelta #20081

Merged
merged 3 commits into from Oct 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions numpy/core/src/multiarray/arraytypes.c.src
Expand Up @@ -2805,11 +2805,9 @@ BOOL_compare(npy_bool *ip1, npy_bool *ip2, PyArrayObject *NPY_UNUSED(ap))

/**begin repeat
* #TYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG,
* DATETIME, TIMEDELTA#
* LONG, ULONG, LONGLONG, ULONGLONG#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_datetime, npy_timedelta#
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
*/

static int
Expand Down Expand Up @@ -2920,6 +2918,37 @@ C@TYPE@_compare(@type@ *pa, @type@ *pb)

/**end repeat**/

/**begin repeat
* #TYPE = DATETIME, TIMEDELTA#
* #type = npy_datetime, npy_timedelta#
*/

static int
@TYPE@_compare(@type@ *pa, @type@ *pb)
{
const @type@ a = *pa;
const @type@ b = *pb;
int ret;

if (a == NPY_DATETIME_NAT) {
if (b == NPY_DATETIME_NAT) {
ret = 0;
}
else {
ret = 1;
}
}
else if (b == NPY_DATETIME_NAT) {
ret = -1;
}
else {
ret = a < b ? -1 : a == b ? 0 : 1;
}
return ret;
}

/**end repeat**/

static int
HALF_compare (npy_half *pa, npy_half *pb, PyArrayObject *NPY_UNUSED(ap))
{
Expand Down