[Numpy-discussion] creation of ndarray with dtype=np.object : bug?
emanuele at relativita.com
Wed Dec 3 05:21:35 EST 2014
On 12/03/2014 04:32 AM, Ryan Nelson wrote:
> This doesn't address your question directly. However, I wonder if you
> could approach this problem from a different way to get what you want.
> First of all, create a "index" array and then just vstack all of your
> arrays at once.
Thank you for your solution. Indeed it works. But it seems to me
that manually creating an index and re-implementing slicing
should be the last resort. NumPy is *great* and provides excellent
slicing and assembling tools. For some reason, that I don't fully
understand, when dtype=np.object the ndarray constructor
tries to be "smart" and creates unexpected results that cannot
Another simple example:
import numpy as np
from numpy.random import rand, randint
n_arrays = 4
shape0_min = 2
shape0_max = 4
for a in range(30):
list_of_arrays = [rand(randint(shape0_min, shape0_max), 3) for i in
array_of_arrays = np.array(list_of_arrays, dtype=np.object)
print("shape: %s" % (array_of_arrays.shape,))
the usual output is:
but from time to time, when the randomly generated arrays have - by chance - the
same shape, you get:
shape: (4, 2, 3)
which may crash your code at runtime.
To NumPy developers: is there a specific reason for np.array(..., dtype=np.object)
to be "smart" instead of just assembling an array with the provided objects?
More information about the NumPy-Discussion