[Numpy-discussion] Less dimensions than expected with record array

Alan Gibson dyssident at gmail.com
Fri Apr 29 22:56:22 EDT 2011


Hello all,

This question may seem elementary (mostly because it is), but I can't
find documentation anywhere as to why the following are true:

>>> import numpy as np
>>> data = [(1,2,3),(4,5,6),(7,8,9)]
>>> dt = [('a',int),('b',int),('c',int)]
>>> normal_array = np.array(data)
>>> record_array = np.array(data, dtype=dt)
>>> print "ndarray has shape %s but record array has shape %s" % \
...     (normal_array.shape, record_array.shape)
ndarray has shape (3, 3) but record array has shape (3,)
>>> print "ndarray has %s dimensions but record array has %s dimensions" % \
...     (normal_array.ndim, record_array.ndim)
ndarray has 2 dimensions but record array has 1 dimensions

This makes seemingly reasonable things, like using apply_along_axis()
over a table of data with named columns, impossible:

>>> np.apply_along_axis(record_array, 1, lambda x: x)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python2.6/dist-packages/numpy/lib/shape_base.py",
line 72, in apply_along_axis
   % (axis,nd))
ValueError: axis must be less than arr.ndim; axis=1, rank=0.

What's the reason for this behavior? Is there a way to make such
operations work with record arrays?

Thanks,

Alan



More information about the NumPy-Discussion mailing list