hatmatrix
hatmatrix

Reputation: 44862

How to create a numpy record array?

This gives me an error:

import numpy as np
x = np.array([[1, 'O', 1]],
             dtype=np.dtype([('step', 'int32'),
                             ('symbol', '|S1'),
                             ('index', 'int32')]))

TypeError: expected a readable buffer object

I don't know why this should fail?

Alternatlively, how can I force something like this statement to work?

x = np.array([[1, 'O', 1]])

then

x.dtype = np.dtype([('step', 'int32'),('symbol', '|S1'),('index', 'int32')])

or

x.view(dtype=np.dtype([('step', 'int32'),('symbol', '|S1'),('index', 'int32')]))

both give me

ValueError: new type not compatible with array.

Edit

If I try to enter each record as a tuple, it will think that the triple is a single value, rather than three separate fields? For instance:

import numpy as np
x = np.array([(1, 'O', 1)],
             dtype=np.dtype([('step', 'int32'),
                             ('symbol', '|S1'),
                             ('index', 'int32')]))

seems fine until I do this:

import numpy.lib.recfunctions as rec
rec.append_fields(x,'x',x['index']+1)

gives me

TypeError: object of type 'numpy.int32' has no len()

presumably because x.shape is (1,) rather than (1,3).

Upvotes: 8

Views: 10148

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222481

I will show a more general way of creating record array:

# prepare the array with different types
recarr = np.zeros((4,), dtype=('i4,f4,a10'))

# creating the columns
col1 = [1, 7, 2, 3]
col2 = [1.1, 0.5, 2, 7.45]
col3 = ['This', 'is', 'text', '!!!']

# create a list of tuples from columns
# prepare = zip(col1, col2, col3)  # Python 2

prepare = list(zip(col1, col2, col3))  # Python 3

# assigning value so recarr
recarr[:] = prepare

Now you can assign names for each of the columns:

recarr.dtype.names = ('ID' , 'price', 'text')

and later get the values for this column:

print recarr('price')

Upvotes: 2

unutbu
unutbu

Reputation: 879181

Make each row a tuple, not a list:

import numpy as np
x = np.array([(1, 'O', 1)],
             dtype=np.dtype([('step', 'int32'),
                             ('symbol', '|S1'),
                             ('index', 'int32')]))

Numpy developer Robert Kern explains:

As a rule, tuples are considered "scalar" records and lists are recursed upon. This rule helps numpy.array() figure out which sequences are records and which are other sequences to be recursed upon; i.e. which sequences create another dimension and which are the atomic elements.

Upvotes: 7

Related Questions