For a long time, I always use
i = 123
x = np.array(i, dtype=np.int)
print x # array(123)
print x.shape # ()
print x.size # 0
print np.array().nbytes # 0
print np.array(123).nbytes # 8
print type(np.array(123)) # numpy.ndarray
I see this
0d case as a natural continuation of
nd. MATLAB makes 2d the lower bound.
numpy could have used
1d, but instead chose
An array consists of a data buffer, whether the value bytes are stored, a dtype (how to interpret those bytes), and
shape is (displayed as) a tuple. Python allows tuples to have 0, 1, 2 or more elements, so why shouldn't shape have the same flexibility?
Look at what
res =  for ary in arys: ary = asanyarray(ary) if len(ary.shape) == 0: result = ary.reshape(1) else: result = ary res.append(result) if len(res) == 1: return res else: return res
It can work with a list of inputs (scalar, array,list etc)
In : np.atleast_1d(np.array(1),np.array(),np.array([])) Out: [array(), array(), array([])]
It converts each to array (as needed) and then checks the dim (len of shape). If 0d it reshapes it to (1,). This reshape does not change the data buffer.
result = ary.reshape(1, 1).
You could also
In : np.array(1,ndmin=1) Out: array()
np.array(1) is in many ways like
np.int32(1). Both have
() shape, both have methods like
sum(). The only obvious difference is in their print format.
I don't know of any reason to purposefully construct a 0d array. It's just as easy to write
np.array() if I really want a 1d array. But you should know how to handle one if it comes up. That includes using
.item() to extract the scalar value, and indexing with
I've encountered it most often in SO questions about loading MATLAB files with
MATLAB constructs are returned as 0d object arrays.
Another way of thinking about a 0d array is that it adds (or retains) the whole suit of array methods to a scalar - without having to explicitly specify the
I mentioned the similarity to
np.int32(1). I've seen it in beginner's code, but have not needed it myself.