Skip to content

How to restructure arrays by adding fields #1549

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

You must be logged in to vote

What you're getting is nested: "tuple of (record of x and y) and (record of z)". A "tuple" is a record without field names (these two fields are "0" and "1"), so this is records-within-records because you constructed a record array out of two record arrays.

You can flatten the field structure like this:

new_array = ak.zip({"x": array1.x, "y": array1.y, "z": array2.z})

or more automatically like this:

new_array = ak.zip(dict(zip(ak.fields(array1), ak.unzip(array1))) | dict(zip(ak.fields(array2), ak.unzip(array2))))

using the dict1 | dict2 syntax from Python 3.9+, or if you have an older Python, there's {**dict1, **dict2}.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@fstrug
Comment options

@jpivarski
Comment options

Answer selected by fstrug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1547 on July 14, 2022 17:49.