-
Hi, If I have a list of awkward arrays, for example:
And I would like to combine the elements of the list, such that I have
Is there a function which does that? I tried to use
Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Now I wish the string representation of Awkward Arrays and Records didn't look so much like Python lists and dicts, because Python data type is important for this question! ak.concatenate is the right function to use, though you can only use it without qualification if you really do have two Awkward Arrays: >>> one = ak.Array([{"x": 20, "y": [1, 2, 3, 4, 5]}])
>>> two = ak.Array([{"x": 30, "y": [6, 7, 8, 9, 10]}])
>>> onetwo = ak.concatenate([one, two])
>>> onetwo.tolist()
[{'x': 20, 'y': [1, 2, 3, 4, 5]}, {'x': 30, 'y': [6, 7, 8, 9, 10]}] You wanted the x data and the y data in separate arrays, so >>> onetwo.x
<Array [20, 30] type='2 * int64'>
>>> onetwo.y
<Array [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] type='2 * var * int64'> or maybe >>> ak.Record({"x": onetwo.x, "y": onetwo.y}).tolist()
{'x': [20, 30], 'y': [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]} To end up with lists of field names, I think you might be iterating over Python dicts or Awkward Records, rather than concatenating Arrays. Maybe this? >>> one = ak.Record({"x": 20, "y": [1, 2, 3, 4, 5]})
>>> two = ak.Record({"x": 30, "y": [6, 7, 8, 9, 10]})
>>> ak.concatenate([one, two])
<Array ['x', 'y', 'x', 'y'] type='4 * string'> Like all functions that take ak.Array input, if ak.concatenate gets an argument that is not an ak.Array, it will try to coerce it into being an ak.Array. A NumPy array, for instance, would be converted with ak.from_numpy. Anything with no recognizable type other than being a Python iterable will be iterated over, using ak.from_iter. ak.Record, like a Python dict, iterates over its field names: >>> list({"x": 20, "y": [1, 2, 3, 4, 5]})
['x', 'y']
>>> list(ak.Record({"x": 20, "y": [1, 2, 3, 4, 5]}))
['x', 'y'] |
Beta Was this translation helpful? Give feedback.
-
Ha! That's exactly what was happening. I'm a bit confused as to why I ended up with a Thank you so much, that's been extremely helpful! |
Beta Was this translation helpful? Give feedback.
Now I wish the string representation of Awkward Arrays and Records didn't look so much like Python lists and dicts, because Python data type is important for this question!
ak.concatenate is the right function to use, though you can only use it without qualification if you really do have two Awkward Arrays:
You wanted the x data and the y data in separate arrays, so