cast_to_min_dtype¶

cast_to_min_dtype(array)[source]¶

Cast the input array to the minimal data type required to represent its values.

This function determines the maximum value in the array and casts it to the smallest unsigned integer type (or boolean) that can accommodate all values. It supports both NumPy and Dask arrays and preserves the input type in the output.

For Dask arrays, the maximum value is computed lazily and only when needed.

Parameters:

array (Union[np.ndarray, da.Array]) – Input array containing integer values.

Returns:

A copy of the input array cast to the minimal required dtype. - If the maximum value is 1, the array is cast to boolean. - Otherwise, it is cast to the smallest suitable unsigned integer type.

Return type:

(np.ndarray or da.Array)