From 982ab6dad08e55c3ed36ddc5d934ad47033f0544 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 11 Apr 2020 20:50:06 -0500 Subject: [PATCH] Implement pd.NA checking function --- pandas/_libs/missing.pyx | 27 +++++++++++++++++++++++++++ pandas/tests/dtypes/test_missing.py | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/pandas/_libs/missing.pyx b/pandas/_libs/missing.pyx index dacf454824190..d18dad343c7c0 100644 --- a/pandas/_libs/missing.pyx +++ b/pandas/_libs/missing.pyx @@ -129,6 +129,33 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr): return result.view(np.bool_) +cpdef ndarray[uint8_t] ispdna(ndarray arr): + """ + Return boolean mask denoting which elements of a 1-D array are pd.NA. + + Parameters + ---------- + arr : ndarray + + Returns + ------- + result : ndarray (dtype=np.bool_) + """ + + cdef: + Py_ssize_t i, n + ndarray[uint8_t] result + + assert arr.ndim == 1, "'arr' must be 1-D." + + n = len(arr) + result = np.empty(n, dtype=np.uint8) + for i in range(n): + result[i] = arr[i] is C_NA + + return result.view(np.bool_) + + @cython.wraparound(False) @cython.boundscheck(False) def isnaobj_old(arr: ndarray) -> ndarray: diff --git a/pandas/tests/dtypes/test_missing.py b/pandas/tests/dtypes/test_missing.py index cad46d0a23967..99c86756e9552 100644 --- a/pandas/tests/dtypes/test_missing.py +++ b/pandas/tests/dtypes/test_missing.py @@ -599,3 +599,9 @@ def test_is_null_datetimelike(self): for value in never_na_vals: assert not is_null_datetimelike(value) + + def test_ispdna(self): + values = np.array([0, np.nan, None, pd.NaT, pd.NA]) + result = libmissing.ispdna(values) + expected = np.array([False, False, False, False, True], dtype=np.bool_) + tm.assert_numpy_array_equal(result, expected)