check_2D_str_array#
- pybear.base.check_2D_str_array(X, require_all_finite=False)#
Validate things that are expected to be 2D arrays of strings.
Accepts 2D Python built-ins, numpy arrays, pandas dataframes, and polars dataframes. Python built-ins can be ragged. When require_all_finite is True, every element in the array must be an instance of str; 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 str. If all checks pass then None is returned.
- Parameters:
- XXContainer[str]
Something that is expected to be a 2D array of strings.
- 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
- XContainer:
Python2DTypes | Numpy2DTypes | Pandas2DTypes | Polars2DTypes
Examples
>>> from pybear.base import check_2D_str_array >>> import numpy as np >>> X = np.random.choice(list('abcde'), (37, 13)).astype('<U4') >>> X[0][8] = 'nan' >>> X[31][3] = '-inf' >>> check_2D_str_array(X, require_all_finite=False) >>> try: ... check_2D_str_array(X, require_all_finite=True) ... except ValueError as e: ... print(e) Got non-finite values when not allowed.