def _resize_with_dtype(arr, dtype):
    """
    This function will transform arr into an array with the same type as dtype. It will do this by
    filling new columns with zeros (or NaNs, if it is a float column). Also, columns that are not
    in the new dtype will be dropped.
    """
    structured_arrays = dtype.names is not None and arr.dtype.names is not None
    old_columns = arr.dtype.names or []
    new_columns = dtype.names or []

    # In numpy 1.9 the ndarray.astype method used to handle changes in number of fields. The code below
    # should replicate the same behaviour the old astype used to have.
    #
    # One may be tempted to use np.lib.recfunctions.stack_arrays to implement both this step and the
    # concatenate that follows but it 2x slower and it requires providing your own default values (instead
    # of np.zeros).
    #
    # Numpy 1.14 supports doing new_arr[old_columns] = arr[old_columns], which is faster than the code below
    # (in benchmarks it seems to be even slightly faster than using the old astype). However, that is not
    # supported by numpy 1.9.2.
    if structured_arrays and (old_columns != new_columns):
        old_columns = set(old_columns)
        new_columns = set(new_columns)

        new_arr = np.zeros(arr.shape, dtype)
        for c in old_columns & new_columns:
            new_arr[c] = arr[c]

        # missing float columns should default to nan rather than zero
        _is_float_type = lambda _dtype: _dtype.type in (np.float32, np.float64)
        _is_void_float_type = lambda _dtype: _dtype.type == np.void and _is_float_type(_dtype.subdtype[0])
        _is_float_or_void_float_type = lambda _dtype: _is_float_type(_dtype) or _is_void_float_type(_dtype)
        _is_float = lambda column: _is_float_or_void_float_type(dtype.fields[column][0])
        for new_column in filter(_is_float, new_columns - old_columns):
            new_arr[new_column] = np.nan

        return new_arr.astype(dtype)
    else:
        return arr.astype(dtype)
def _resize_with_dtype ( arr , dtype ) : structured_arrays = dtype . names is not None and arr . dtype . names is not None old_columns = arr . dtype . names or [ ] new_columns = dtype . names or [ ] if structured_arrays and ( old_columns != new_columns ) : old_columns = set ( old_columns ) new_columns = set ( new_columns ) new_arr = np . zeros ( arr . shape , dtype ) for c in old_columns & new_columns : new_arr [ c ] = arr [ c ] _is_float_type = lambda _dtype : _dtype . type in ( np . float32 , np . float64 ) _is_void_float_type = lambda _dtype : _dtype . type == np . void and _is_float_type ( _dtype . subdtype [ 0 ] ) _is_float_or_void_float_type = lambda _dtype : _is_float_type ( _dtype ) or _is_void_float_type ( _dtype ) _is_float = lambda column : _is_float_or_void_float_type ( dtype . fields [ column ] [ 0 ] ) for new_column in filter ( _is_float , new_columns - old_columns ) : new_arr [ new_column ] = np . nan return new_arr . astype ( dtype ) else : return arr . astype ( dtype )
This function will transform arr into an array with the same type as dtype. It will do this by
    filling new columns with zeros (or NaNs, if it is a float column). Also, columns that are not
    in the new dtype will be dropped.