-
Link to page: https://awkward-array.org/how-to-restructure-add-fields.html I need some further documentation so that I can understand how to add a new "branch" to an awkward array that comes from an opened TTree using uproot. It seems that TTrees opened in uproot follow a similar structure to this example:
How would I go about adding a new field to this array such that it is properly indexed/formatted with the original array. If we try:
Now if we try combine array1 and array2 together.
We get this output |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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 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 |
Beta Was this translation helpful? Give feedback.
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:
or more automatically like this:
using the
dict1 | dict2
syntax from Python 3.9+, or if you have an older Python, there's{**dict1, **dict2}
.