nan_mask_numerical#

pybear.utilities.nan_mask_numerical(X)#

Return a boolean numpy array or vector indicating the locations of nan-like representations in the data.

“nan-like representations” include, at least, numpy.nan, pandas.NA, None, and string representations of “nan”. In the cases of Python native, numpy, pandas, and polars objects of shape (n_samples, n_features) or (n_samples, ), return an identically shaped numpy array. In the cases of scipy sparse objects, return a vector with shape equal to that of the ‘data’ attribute of the sparse object.

This function accepts Python lists, tuples, and sets, numpy arrays, pandas dataframes and series, polars dataframes and series, and all scipy sparse matrices/arrays except dok and lil formats. It does not accept any ragged Python built-ins, numpy recarrays, or numpy masked arrays. Data must be able to cast to numpy numerical dtypes.

Parameters:
XXContainer of shape (n_samples, n_features) or (n_samples, )

The object for which to locate nan-like representations.

Returns:
masknumpy.ndarray[bool]

shape (n_samples, n_features) or (n_samples, ) or (n_non_zero_values, )

Indicates nan-like representations in X via the value boolean True. Values that are not nan-like are False.

Notes

Type Aliases

PythonTypes:

list | tuple | set | list[list] | tuple[tuple]]

NumpyTypes:

numpy.ndarray

PandasTypes:

pandas.DataFrame | pandas.Series

PolarsTypes:

polars.DataFrame | polars.Series

ScipySparseTypes:

ss._csr.csr_matrix | ss._csc.csc_matrix | ss._coo.coo_matrix | ss._dia.dia_matrix | ss._bsr.bsr_matrix | ss._csr.csr_array | ss._csc.csc_array | ss._coo.coo_array | ss._dia.dia_array | ss._bsr.bsr_array

XContainer:

PythonTypes | NumpyTypes | PandasTypes | PolarsTypes | ScipySparseTypes

Examples

>>> from pybear.utilities import nan_mask_numerical
>>> import numpy as np
>>> X = np.arange(6).astype(np.float64)
>>> X[1] = np.nan
>>> X[-2] = np.nan
>>> X
array([ 0., nan,  2.,  3., nan,  5.])
>>> nan_mask_numerical(X)
array([False,  True, False, False,  True, False])