Skip to content

Best way to create a new deeply nested array with shape and depth matching an existing array? #3101

Answered by jpivarski
raymondEhlers asked this question in Q&A
Discussion options

You must be logged in to vote

What you're proposing as ak.shape_like is essentially ak.unflatten, if it worked on multiple levels at once. And if it took an input array's structure directly, instead of asking for counts (which could be taken from ak.num).

Actually,

def shape_like(flat_array, array_with_desired_structure):
    flat_array_as_layout = ak.to_layout(flat_array)
    # flat_array needs to be flat
    assert flat_array_as_layout.is_numpy

    def transformation(layout, **kwargs):
        # this function only makes sense for non-branching layouts
        assert not layout.is_record and not layout.is_union

        if layout.is_numpy:
            # they need to have the same number of numerical values
            

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@raymondEhlers
Comment options

@jpivarski
Comment options

@raymondEhlers
Comment options

@jpivarski
Comment options

Answer selected by raymondEhlers
@raymondEhlers
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants