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

Christopher Barker Chris.Barker at noaa.gov
Mon May 2 12:17:15 EDT 2011


>> 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:

> each row (record) is treated as one array element, so the structured
> array is only 1d.
>
> If you have rows/records with content that is not homogenous, then
> working along axis=1 (across elements of a record) doesn't make sense.
> for example I just struggle with 2 datetime columns and the rest are
> integers.
>
> If you want an array with homogenous elements (all floats or all ints)
> with operations along axis, then larry (la) is, I think, still the
> best bet.

another option is to use views. There are time when I want the same 
array visible as both a structured array, and a regular old array, 
depending on what I'm doing with it. and you can do that:

In [77]: data
Out[77]: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [80]: dt = [('a',int),('b',int),('c',int)]

In [81]: record_array = np.array(data, dtype=dt)

In [84]: array = record_array.view(dtype=np.int).reshape(-1,3)

In [85]: array
Out[85]:
array([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

# array and record_array share the same data:

In [88]: array[:,1] *= 2

In [89]: array
Out[89]:
array([[ 1,  4,  3],
        [ 4, 10,  6],
        [ 7, 16,  9]])

In [90]: record_array
Out[90]:
array([(1, 4, 3), (4, 10, 6), (7, 16, 9)],
       dtype=[('a', '<i4'), ('b', '<i4'), ('c', '<i4')])


views are one of the really cool things about numpy.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list