[Numpy-discussion] Request for a bit more info on structured arrays in the "basics" page

Skipper Seabold jsseabold at gmail.com
Sat Mar 5 12:10:21 EST 2011

On Sat, Mar 5, 2011 at 9:28 AM, Ralf Gommers
<ralf.gommers at googlemail.com> wrote:
> On Sat, Mar 5, 2011 at 8:09 AM, Russell E. Owen <rowen at uw.edu> wrote:
>> The page <http://docs.scipy.org/doc/numpy/user/basics.rec.html>
>> gives a good introduction to structured arrays. However, it says nothing
>> about how to set a particular element (all fields at once) from a
>> collection of data.
>> For instance:
>> stArr = numpy.zeros([4,5], dtype=[("pos", float, (2,)), ("rot", float)])
>> The question is how to set stArr[0]?
>> >From experimentation it appears that you can provide a tuple, but not a
>> list. Hence the following works just fine (and that the tuple can
>> contain a list):
>> strArr[0,0] = ([1.0, 1.1], 2.0)
>> but the following fails:
>> strArr[0,0] = [[1.0, 1.1], 2.0]
>> with an error:
>> TypeError: expected a readable buffer object
>> This is useful information if one is trying to initialize a structured
>> array from a collection of data, such as that returned from a database
>> query.

I added a bit at the end here, though it is mentioned briefly above.
Feel free to expand. It's a wiki. You just need edit rights.


> I'm wondering if that's not a bug? If it's intentional then it is
> certainly counterintuitive.

This comes up from time to time.


Perhaps an enhancement ticket could be filed? It doesn't sound trivial
to implement.


More information about the NumPy-Discussion mailing list