Size of data type using NumPy

In NumPy, I can get the size (in bytes) of a particular data type by:

datatype(...).itemsize

or:

datatype(...).nbytes

For example:

np.float32(5).itemsize #4
np.float32(5).nbytes   #4

I have two questions. First, is there a way to get this information without creating an instance of the datatype? Second, what's the difference between itemsize and nbytes?


You need an instance of the dtype to get the itemsize, but you shouldn't need an instance of the ndarray. (As will become clear in a second, nbytes is a property of the array, not the dtype.)

E.g.

print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize

As far as the difference between itemsize and nbytes, nbytes is just x.itemsize * x.size.

E.g.

In [16]: print np.arange(100).itemsize
8

In [17]: print np.arange(100).nbytes
800

Looking at the NumPy C source file, this is the comment:

size : int
    Number of elements in the array.
itemsize : int
    The memory use of each array element in bytes.
nbytes : int
    The total number of bytes required to store the array data,
    i.e., ``itemsize * size``.

So in NumPy:

>>> x = np.zeros((3, 5, 2), dtype=np.float64)
>>> x.itemsize
8

So .nbytes is a shortcut for:

>>> np.prod(x.shape)*x.itemsize
240
>>> x.nbytes
240

So, to get a base size of a NumPy array without creating an instance of it, you can do this (assuming a 3x5x2 array of doubles for example):

>>> np.float64(1).itemsize * np.prod([3,5,2])
240

However, important note from the NumPy help file:

|  nbytes
|      Total bytes consumed by the elements of the array.
|
|      Notes
|      -----
|      Does not include memory consumed by non-element attributes of the
|      array object.