Skip to content
Advertisement

Question about turning a list of numpy arrays into an object array

I have a question about turning a list of numpy arrays into an object array.

JavaScript

This results in two very different outcomes:

JavaScript

I assume that the difference comes from the fact that in testing_2_array not all arrays have the same size. Is there any way to force numpy to output testing_1_array in the same way as testing_2_output so that I do not have to additionally check if all arrays in the initial list have the same size?

Advertisement

Answer

np.array tries, where possible to make a multidimensional numeric dtype array. Creating a ragged object dtype array is a fall back option. And with some combinations of shapes, even that raises an error. Specifying object dtype doesn’t change that fundamental behavior.

Creating a “empty” array and filling it is the most general option.

JavaScript

It also works with the ragged shape:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement