def char_to_bytes(arr):
    """Convert numpy/dask arrays from characters to fixed width bytes."""
    if arr.dtype != 'S1':
        raise ValueError("argument must have dtype='S1'")

    if not arr.ndim:
        # no dimension to concatenate along
        return arr

    size = arr.shape[-1]

    if not size:
        # can't make an S0 dtype
        return np.zeros(arr.shape[:-1], dtype=np.string_)

    if isinstance(arr, dask_array_type):
        import dask.array as da

        if len(arr.chunks[-1]) > 1:
            raise ValueError('cannot stacked dask character array with '
                             'multiple chunks in the last dimension: {}'
                             .format(arr))

        dtype = np.dtype('S' + str(arr.shape[-1]))
        return da.map_blocks(_numpy_char_to_bytes, arr,
                             dtype=dtype,
                             chunks=arr.chunks[:-1],
                             drop_axis=[arr.ndim - 1])
    else:
        return StackedBytesArray(arr)
def char_to_bytes ( arr ) : if arr . dtype != 'S1' : raise ValueError ( "argument must have dtype='S1'" ) if not arr . ndim : return arr size = arr . shape [ - 1 ] if not size : return np . zeros ( arr . shape [ : - 1 ] , dtype = np . string_ ) if isinstance ( arr , dask_array_type ) : import dask . array as da if len ( arr . chunks [ - 1 ] ) > 1 : raise ValueError ( 'cannot stacked dask character array with ' 'multiple chunks in the last dimension: {}' . format ( arr ) ) dtype = np . dtype ( 'S' + str ( arr . shape [ - 1 ] ) ) return da . map_blocks ( _numpy_char_to_bytes , arr , dtype = dtype , chunks = arr . chunks [ : - 1 ] , drop_axis = [ arr . ndim - 1 ] ) else : return StackedBytesArray ( arr )
Convert numpy/dask arrays from characters to fixed width bytes.