8

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).

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but then the shape of x is (1,) rather than (1,3)?
That's what you are supposed to get when defining a structured array. You can access the columns with syntax like: x['symbol']
Thanks I'll post the appending field as a separate question.
2

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.