Skip to content

Commit b63b156

Browse files
committed
properly use PyArray_IsScalar before PyArrayScalar_VAL
1 parent 3de2c63 commit b63b156

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/_arraykit.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,10 +3777,19 @@ isna_element(PyObject *m, PyObject *args, PyObject *kwargs)
37773777
if (!PyCallable_Check(to_numpy)) {
37783778
Py_RETURN_FALSE;
37793779
}
3780-
PyObject* post = PyObject_CallFunction(to_numpy, NULL);
3780+
3781+
PyObject* scalar = PyObject_CallFunction(to_numpy, NULL);
37813782
Py_DECREF(to_numpy);
3782-
if (post == NULL) return NULL;
3783-
return PyBool_FromLong(PyArrayScalar_VAL(post, Datetime) == NPY_DATETIME_NAT);
3783+
if (scalar == NULL) {
3784+
return NULL;
3785+
}
3786+
if (!PyArray_IsScalar(scalar, Datetime)) {
3787+
Py_DECREF(scalar);
3788+
Py_RETURN_FALSE;
3789+
}
3790+
PyObject* pb = PyBool_FromLong(PyArrayScalar_VAL(scalar, Datetime) == NPY_DATETIME_NAT);
3791+
Py_DECREF(scalar);
3792+
return pb;
37843793
}
37853794
Py_RETURN_FALSE;
37863795
}

test/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ def test_isna_element_d(self) -> None:
351351
ts = pd.Timestamp('nat')
352352
self.assertTrue(isna_element(ts))
353353

354+
s1 = pd.Series((0,))
355+
self.assertFalse(isna_element(s1))
356+
357+
354358

355359
def test_isna_element_e(self) -> None:
356360
from types import SimpleNamespace

0 commit comments

Comments
 (0)