Skip to content

Commit 803ff35

Browse files
authored
Merge pull request #107 from static-frame/106/isna-element
`isna_element()` improvements
2 parents 3de2c63 + e312b33 commit 803ff35

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/_arraykit.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,17 +3770,29 @@ isna_element(PyObject *m, PyObject *args, PyObject *kwargs)
37703770
}
37713771
// Try to identify Pandas Timestamp NATs
37723772
if (PyObject_HasAttrString(element, "to_numpy")) {
3773-
PyObject *to_numpy = PyObject_GetAttrString(element, "to_numpy");
3774-
if (to_numpy == NULL) {
3775-
return NULL;
3776-
}
3777-
if (!PyCallable_Check(to_numpy)) {
3778-
Py_RETURN_FALSE;
3779-
}
3780-
PyObject* post = PyObject_CallFunction(to_numpy, NULL);
3781-
Py_DECREF(to_numpy);
3782-
if (post == NULL) return NULL;
3783-
return PyBool_FromLong(PyArrayScalar_VAL(post, Datetime) == NPY_DATETIME_NAT);
3773+
// strcmp returns 0 on match
3774+
return PyBool_FromLong(strcmp(element->ob_type->tp_name, "NaTType") == 0);
3775+
// the long way
3776+
// PyObject *to_numpy = PyObject_GetAttrString(element, "to_numpy");
3777+
// if (to_numpy == NULL) {
3778+
// return NULL;
3779+
// }
3780+
// if (!PyCallable_Check(to_numpy)) {
3781+
// Py_DECREF(to_numpy);
3782+
// Py_RETURN_FALSE;
3783+
// }
3784+
// PyObject* scalar = PyObject_CallFunction(to_numpy, NULL);
3785+
// Py_DECREF(to_numpy);
3786+
// if (scalar == NULL) {
3787+
// return NULL;
3788+
// }
3789+
// if (!PyArray_IsScalar(scalar, Datetime)) {
3790+
// Py_DECREF(scalar);
3791+
// Py_RETURN_FALSE;
3792+
// }
3793+
// PyObject* pb = PyBool_FromLong(PyArrayScalar_VAL(scalar, Datetime) == NPY_DATETIME_NAT);
3794+
// Py_DECREF(scalar);
3795+
// return pb;
37843796
}
37853797
Py_RETURN_FALSE;
37863798
}

test/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,17 @@ 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+
354357

355358
def test_isna_element_e(self) -> None:
356359
from types import SimpleNamespace
357360
sn = SimpleNamespace()
358361
sn.to_numpy = None
359362
self.assertFalse(isna_element(sn))
360363

364+
361365
#---------------------------------------------------------------------------
362366

363367
def test_dtype_from_element_core_dtypes(self) -> None:

0 commit comments

Comments
 (0)