In Numpy, I want to create an array of integer arrays (or lists). Each individual array is a set of indices. These individual arrays generally have different lengths, but sometimes all have the same length.
When the lengths are different, I can create the array as
test = np.array([[1,2],[1,2,3]],dtype=object)
When I do this, test[0]
is a list of integers and I can use other_array[test[0]]
without issue.
However, when test
happens to have entries all the same size and I do
test = np.array([[1,2],[1,3]], dtype=object)
then test[0]
is a Numpy array of dtype object
. When I use other_array[test[0]]
I get an error that arrays used as indices must be of integer (or boolean) type
.
Here is a complete example:
other_array = np.array([0,1,2,3])
test1 = np.array([[1,2],[1,2,3]], dtype=object)
print(other_array[test1[0]]) #this works
test2 = np.array([[1,2],[1,3]], dtype=object)
print(other_array[test2[0]]) #this fails
The only way I have found around this issue is to check if test
will be ragged or not before creating it and use dtype=int
when it happens to have arrays of all the same size. This seems inefficient. Is there a generic way to create an array of integer arrays that is sometimes ragged and sometimes not without checking for raggedness?