Search code examples
pythonawkward-array

append an element to every row of a jagged array


I am trying to add a 0 to every row of a jagged array. I want to go from

<JaggedArray [[1 2 3] [1 2]]>

to

<JaggedArray [[1 2 3 0] [1 2 0]]>

so that when I grab the -1th index, I get 0. Currently I'm padding every row to the length of the biggest row + 1, then filling nans with 0, which works, but I am wondering if there's a better way.

I saw that there's a class AppendableArray that has a .append() function, but I'm not sure how to convert between the two. I'm using awkward 0.12.22, and the data is read out of a ROOT file with uproot 3.11.0


Solution

  • Perhaps this is too short to be an answer, but

    1. Upgrade to Awkward 1.x (you can still import awkward0 and use ak.from_awkward0 and ak.to_awkward0 to go back and forth in the same process).
    2. Create an array of single-item lists, perhaps in NumPy (ak.from_numpy), perhaps by slicing a one-dimensional array with np.newaxis.
    3. Concatenate it with your other array using ak.concatenate with axis=1. The first dimension needs to be the same (len of both arrays must be equal), but the second dimensions are unconstrained.