check_2D_num_array#
- pybear.base.check_2D_num_array(X, require_all_finite=False)#
Validate things that are expected to be 2D arrays of numbers.
Accepts 2D Python built-ins, numpy arrays, pandas dataframes, polars dataframes, and all scipy sparse matrices/arrays. Python built-ins can be ragged. When require_all_finite is True, every element in the array must be an instance of numbers.Number; a ValueError will be raised if there are any nan-like or infinity-like values. If require_all_finite is False, non-finite values are ignored and only the finite values must be an instance of numbers.Number. If all checks pass then None is returned.
- Parameters:
- XXContainer[numbers.Number]
Something that is expected to be a 2D array of numbers.
- require_all_finitebool, default=False
If True, disallow all non-finite values, such as nan-like or infinity-like values.
- Returns:
- None
- Raises:
- TypeError:
For invalid container.
- ValueError:
For non-finite values when require_all_finite is True.
Notes
Type Aliases
- Python2DTypes:
list[list] | tuple[tuple]
- Numpy2DTypes:
numpy.ndarray
- Pandas2DTypes:
pandas.DataFrame
- Polars2DTypes:
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:
Python2DTypes | Numpy2DTypes | Pandas2DTypes | Polars2DTypes | ScipySparseTypes
Examples
>>> from pybear.base import check_2D_num_array >>> import numpy as np >>> X = np.random.randint(0, 10, (37, 13)).astype(np.float64) >>> X[0][8] = np.nan >>> X[31][3] = np.inf >>> check_2D_num_array(X, require_all_finite=False) >>> try: ... check_2D_num_array(X, require_all_finite=True) ... except ValueError as e: ... print(e) Got non-finite values when not allowed.