check_dtype#

pybear.base.check_dtype(X, allowed='any', require_all_finite=False)#

Check that the passed data is of an allowed datatype.

If not, raise TypeError. Allowed dtypes are ‘any’, ‘numeric’, and ‘str’. If all checks pass then return None.

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

The data to be checked for allowed datatype.

allowedLiteral[‘numeric’, ‘str’, ‘any’], default=’any’

The allowed datatype for the data. If ‘numeric’, only allow values that are instances of numbers.Number. If not, raise TypeError. If ‘str’, all data in X must be and instance of str or a TypeError is raised. If ‘any’, allow any datatype.

require_all_finitebool, default=False

If True, raise an exception if there are any nan-like or infinity-like values in the data. This means that all elements in the data must be of the required dtype. If False, nan-likes and infinity-likes are allowed, and all other values (the finite values) must be of the required dtype.

Returns:
None

Notes

Type Aliases

PythonTypes:

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

NumpyTypes:

numpy.ndarray

PandasTypes:

pandas.Series | pandas.DataFrame

PolarsTypes:

polars.Series | polars.DataFrame

ScipySparseTypes:

ss.csc_matrix | ss.csc_array | ss.csr_matrix | ss.csr_array | ss.coo_matrix | ss.coo_array | ss.dia_matrix | ss.dia_array | ss.lil_matrix | ss.lil_array | ss.dok_matrix | ss.dok_array | ss.bsr_matrix | ss.bsr_array

XContainer:

PythonTypes | NumpyTypes | PandasTypes | PolarsTypes | ScipySparseTypes

Examples

>>> from pybear.base import check_dtype
>>> X = [1, 3, 5, np.nan]
>>> check_dtype(X, allowed='numeric', require_all_finite=False)
>>> try:
...     check_dtype(X, allowed='str', require_all_finite=False)
... except TypeError as e:
...     print(repr(e))
TypeError('Expected a 1D sequence of string-like values. ')
>>> try:
...     check_dtype(X, allowed='numeric', require_all_finite=True)
... except ValueError as e:
...     print(repr(e))
ValueError('Got non-finite values when not allowed.')