verdict.py
def jagged_arr_to_filled_arr( arr, fill_value=None, dtype=None, ):
    '''Convert a jagged array to a uniform filled array of minimum size
    needed to contain all the data.

    Args:
        arr (array-like):
            Jagged array to convert to a filled array.

        fill_value:
            Fill value to use. Defaults to -9999 for integers, NaN otherwise.

        dtype:
            Datatype. Defaults to the datatype of arr, if arr consists of data
            of one type.

    Returns:
        np n-dim array:
            A filled array with minimum dimensions needed to contain arr.
    '''

    def arr_depth( a, level=1 ):
        '''Get the array depth, even for a jagged array.'''

        if len( a ) == 0:
            return level

        depths = []
        for v in a:
            if pd.api.types.is_list_like( v ):
                depths.append( arr_depth( v, level+1 ) )
            else:
                depths.append( level )

        return max( depths )

    def recursive_array_shape( a, s=[], dtypes=[], level=0 ):
        '''Loop through and get max dimensions and datatypes of jagged array'''

        # Get length at current depth
        len_depth = np.array( a ).shape[0]

        # Compare to s
        if len( s ) > level:
            s[level] = max( s[level], len_depth )
        else:
            s.append( len_depth )

        # Recurse
        for v in a:
            if not pd.api.types.is_list_like( v ):
                if type( v ) not in dtypes:
                    if isinstance( v, str ):
                        dtype = len( v )
                    else:
                        dtype = type( v )
                    dtypes.append( dtype )
                continue
            s, dtypes = recursive_array_shape( v, s, dtypes, level+1 )

        return s, dtypes
            
    shape, dtypes = recursive_array_shape( arr )

    # The two sections below are somewhat complicated as a result of these:
    # a) The full length of all strings must be saved.
    # b) Passing dtype to np.full can result in unexpected behavior.
    # c) np.dtype( int ).type( np.nan ) is overly long and complicated.

    # Choose array type
    str_dtype = not False in [ isinstance( _, int ) for _ in dtypes ]
    if str_dtype:
        # The + [3,] gives a minimum length
        dtype = '<U{}'.format( max( dtypes+[ 3, ] ) )
    elif len( dtypes ) > 1 and dtype is None:
        raise TypeError( 'Must choose dtype for jagged arrays with multiple data types.' )
    else:
        dtype = dtypes[0]

    # Choose automatic fill value
    if fill_value is None:
        if str_dtype:
            arr_dtype = dtype
            fill_value = np.nan
        else:
            arr_dtype = np.float64
            try:
                fill_value = dtype( np.nan )
            except ValueError:
                fill_value = dtype( -9999 )
    else:
        arr_dtype = type( fill_value )

    new_arr = np.full( shape, fill_value, dtype=arr_dtype )

    def store_jagged_to_masked( a, m_a, ):
        '''Actually store the jagged array to the masked array.'''

        if arr_depth( a ) == 2:
            for i, v in enumerate( a ):
                m_a[i,:len(v)] = v
            return m_a
        else:
            for i, v in enumerate( a ):
                m_a[i] = store_jagged_to_masked( v, m_a[i] )

        return m_a

    new_arr = store_jagged_to_masked( arr, new_arr )

    return new_arr, fill_value
Convert a jagged array to a uniform filled array of minimum size
    needed to contain all the data.

    Args:
        arr (array-like):
            Jagged array to convert to a filled array.

        fill_value:
            Fill value to use. Defaults to -9999 for integers, NaN otherwise.

        dtype:
            Datatype. Defaults to the datatype of arr, if arr consists of data
            of one type.

    Returns:
        np n-dim array:
            A filled array with minimum dimensions needed to contain arr.
    
Convert a jagged array to a uniform filled array of minimum size
needed to contain all the data.
Convert a jagged array to a uniform filled array of minimum size needed to contain all the data .
def jagged_arr_to_filled_arr( arr, fill_value=None, dtype=None, ):
    def arr_depth( a, level=1 ):
        if len( a ) == 0:
            return level
        depths = []
        for v in a:
            if pd.api.types.is_list_like( v ):
                depths.append( arr_depth( v, level+1 ) )
            else:
                depths.append( level )
        return max( depths )
    def recursive_array_shape( a, s=[], dtypes=[], level=0 ):
        len_depth = np.array( a ).shape[0]
        if len( s ) > level:
            s[level] = max( s[level], len_depth )
        else:
            s.append( len_depth )
        for v in a:
            if not pd.api.types.is_list_like( v ):
                if type( v ) not in dtypes:
                    if isinstance( v, str ):
                        dtype = len( v )
                    else:
                        dtype = type( v )
                    dtypes.append( dtype )
                continue
            s, dtypes = recursive_array_shape( v, s, dtypes, level+1 )
        return s, dtypes
    shape, dtypes = recursive_array_shape( arr )
    str_dtype = not False in [ isinstance( _, int ) for _ in dtypes ]
    if str_dtype:
        dtype = '<U{}'.format( max( dtypes+[ 3, ] ) )
    elif len( dtypes ) > 1 and dtype is None:
        raise TypeError( 'Must choose dtype for jagged arrays with multiple data types.' )
    else:
        dtype = dtypes[0]
    if fill_value is None:
        if str_dtype:
            arr_dtype = dtype
            fill_value = np.nan
        else:
            arr_dtype = np.float64
            try:
                fill_value = dtype( np.nan )
            except ValueError:
                fill_value = dtype( -9999 )
    else:
        arr_dtype = type( fill_value )
    new_arr = np.full( shape, fill_value, dtype=arr_dtype )
    def store_jagged_to_masked( a, m_a, ):
        if arr_depth( a ) == 2:
            for i, v in enumerate( a ):
                m_a[i,:len(v)] = v
            return m_a
        else:
            for i, v in enumerate( a ):
                m_a[i] = store_jagged_to_masked( v, m_a[i] )
        return m_a
    new_arr = store_jagged_to_masked( arr, new_arr )
    return new_arr, fill_value
def jagged_arr_to_filled_arr ( arr , fill_value = None , dtype = None , ) : def arr_depth ( a , level = 1 ) : '''Get the array depth, even for a jagged array.''' if len ( a ) == 0 : return level depths = [ ] for v in a : if pd . api . types . is_list_like ( v ) : depths . append ( arr_depth ( v , level + 1 ) ) else : depths . append ( level ) return max ( depths ) def recursive_array_shape ( a , s = [ ] , dtypes = [ ] , level = 0 ) : '''Loop through and get max dimensions and datatypes of jagged array''' len_depth = np . array ( a ) . shape [ 0 ] if len ( s ) > level : s [ level ] = max ( s [ level ] , len_depth ) else : s . append ( len_depth ) for v in a : if not pd . api . types . is_list_like ( v ) : if type ( v ) not in dtypes : if isinstance ( v , str ) : dtype = len ( v ) else : dtype = type ( v ) dtypes . append ( dtype ) continue s , dtypes = recursive_array_shape ( v , s , dtypes , level + 1 ) return s , dtypes shape , dtypes = recursive_array_shape ( arr ) str_dtype = not False in [ isinstance ( _ , int ) for _ in dtypes ] if str_dtype : dtype = '<U{}' . format ( max ( dtypes + [ 3 , ] ) ) elif len ( dtypes ) > 1 and dtype is None : raise TypeError ( 'Must choose dtype for jagged arrays with multiple data types.' ) else : dtype = dtypes [ 0 ] if fill_value is None : if str_dtype : arr_dtype = dtype fill_value = np . nan else : arr_dtype = np . float64 try : fill_value = dtype ( np . nan ) except ValueError : fill_value = dtype ( - 9999 ) else : arr_dtype = type ( fill_value ) new_arr = np . full ( shape , fill_value , dtype = arr_dtype ) def store_jagged_to_masked ( a , m_a , ) : '''Actually store the jagged array to the masked array.''' if arr_depth ( a ) == 2 : for i , v in enumerate ( a ) : m_a [ i , : len ( v ) ] = v return m_a else : for i , v in enumerate ( a ) : m_a [ i ] = store_jagged_to_masked ( v , m_a [ i ] ) return m_a new_arr = store_jagged_to_masked ( arr , new_arr ) return new_arr , fill_value
Convert a jagged array to a uniform filled array of minimum size
needed to contain all the data.
Convert a jagged array to a uniform filled array of minimum size needed to contain all the data .
'''Convert a jagged array to a uniform filled array of minimum size
    needed to contain all the data.

    Args:
        arr (array-like):
            Jagged array to convert to a filled array.

        fill_value:
            Fill value to use. Defaults to -9999 for integers, NaN otherwise.

        dtype:
            Datatype. Defaults to the datatype of arr, if arr consists of data
            of one type.

    Returns:
        np n-dim array:
            A filled array with minimum dimensions needed to contain arr.
    ''' '''Get the array depth, even for a jagged array.''' '''Loop through and get max dimensions and datatypes of jagged array''' # Get length at current depth # Compare to s # Recurse # The two sections below are somewhat complicated as a result of these: # a) The full length of all strings must be saved. # b) Passing dtype to np.full can result in unexpected behavior. # c) np.dtype( int ).type( np.nan ) is overly long and complicated. # Choose array type # The + [3,] gives a minimum length # Choose automatic fill value '''Actually store the jagged array to the masked array.'''
np n-dim array:
A filled array with minimum dimensions needed to contain arr.
np n - dim array : A filled array with minimum dimensions needed to contain arr .
arr
Jagged array to convert to a filled array.
Jagged array to convert to a filled array .
fill_value
Fill value to use. Defaults to -9999 for integers, NaN otherwise.
Fill value to use . Defaults to - 9999 for integers NaN otherwise .
-9999 for integers, NaN otherwise
dtype
Datatype. Defaults to the datatype of arr, if arr consists of data
of one type.
Datatype . Defaults to the datatype of arr if arr consists of data of one type .