Empty slicing behaviour before and after ak.to_numpy
is different
#1047
-
Version of Awkward Array'1.4.0' Description and code to reproduceDear awkward developers, we noticed an unexpected behaviour in fancy indexing empty dimensions before and after # before
ak.to_numpy(ak.Array([[1], [2]])[[]])
>> array([], shape=(0, 0), dtype=int64)
# after
ak.to_numpy(ak.Array([[1], [2]]))[[]]
>> array([], shape=(0, 1), dtype=int64) # <-- this is what we expected The shape discrepancy is unexpected for us. Best, Peter |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It's because >>> ak.Array([[1], [2]]) # note the "var"
<Array [[1], [2]] type='2 * var * int64'>
>>>
>>> ak.Array(np.array([[1], [2]])) # now it's regular
<Array [[1], [2]] type='2 * 1 * int64'> The empty slice maintains var on the var-type array and the regular length of the regular array: >>> ak.Array([[1], [2]])[[]]
<Array [] type='0 * var * int64'>
>>>
>>> ak.Array(np.array([[1], [2]]))[[]]
<Array [] type='0 * 1 * int64'> And this is preserved when converting to NumPy. Var-type arrays can only be converted to NumPy if they happen to be regular in the instance you're trying to convert. (Regular arrays can always be converted to NumPy.) The lengths of the inner dimensions of a var-type array are determined from what those list lengths happen to be in the particular instance. Here, they happen to be zero because they're empty. >>> ak.to_numpy(ak.Array([[1], [2]])[[]])
array([], shape=(0, 0), dtype=int64)
>>>
>>> ak.to_numpy(ak.Array(np.array([[1], [2]]))[[]])
array([], shape=(0, 1), dtype=int64) It's unfortunate that we have to have this subtlety—that you, as user, have to be aware of the distinction between var-type and regular-type nested lists, but this is because only the regular-type can perfectly correspond to NumPy's behavior; var-type is new territory.
|
Beta Was this translation helpful? Give feedback.
It's because
ak.Array([[1], [2]])
is an array of variable-length lists andnp.array([[1], [2]])
is an array of fixed-length lists:The empty slice maintains var on the var-type array and the regular length of the regular array:
And this is preserved when converting to NumPy. Var-type arrays can only be converted to NumPy if they happen to be regul…