I find myself needing to create a numpy array of dtype="object" whose elements are themselves numpy arrays. I can manage to do this if the arrays are different lengths:
arr_of_arrs = np.empty((2,2), dtype=np.object)
arr_list = [np.arange(i) for i in range(4)]
arr_of_arrs.flat[:] = arr_list
print(arr_of_arrs)
array([[array([], dtype=int32), array([0])],
[array([0, 1]), array([0, 1, 2])]], dtype=object)
But if they happen to be the same length, it doesn't work and I am not entirely sure how it is generating the values it gives me:
arr_list = [np.arange(2) for i in range(4)]
arr_of_arrs.flat[:] = arr_list
print(arr_of_arrs)
[[0 1]
[0 1]]
Is this even doable? numpy seems to try and coerce the data into "making sense" despite my best efforts to prevent it from doing so...
arr_of_arrsstarts as a 1d array, e.g.np.empty(4, object). You can reshape it later if needed.flatis flattening and iterating both sides, which messes up this assignment.arr_of_arrays.ravel()[:]=...also works better.