Skip to content

Implement pd.NA checking function #33490

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a big deal, but is this restriction necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not, the main application I had in mind was when operating on single columns though


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:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)