[Numpy-discussion] Documentation for dtypes with named fields

Skipper Seabold jsseabold at gmail.com
Tue Mar 16 12:29:29 EDT 2010


On Tue, Mar 16, 2010 at 12:23 PM,  <josef.pktd at gmail.com> wrote:
> On Tue, Mar 16, 2010 at 12:04 PM, Sam Tygier
> <Sam.Tygier at hep.manchester.ac.uk> wrote:
>> On Tue, Mar 16, 2010 at 11:34 AM, Skipper Seabold <jsseabold at gmail.com> wrote:
>>>> so i always get the vales back in the original order. is the by design, or a bug?
>>>>
>>>
>>> I've been bitten by this before too and asked the same question with
>>> no response.  I think it's just a limitation of the design of
>>> structured arrays.
>>
>> i had a hunt for the code. and it seems easy to fix.
>> its in numpy/core/_internal.py:301
>>
>> --- numpy/core/_internal.py     2010-03-16 16:01:28.000000000 +0000
>> +++ numpy/core/_internal.py.old 2010-03-16 16:00:52.000000000 +0000
>> @@ -298,7 +298,7 @@
>>  def _index_fields(ary, fields):
>>     from multiarray import empty, dtype
>>     dt = ary.dtype
>> -    new_dtype = [(name, dt[name]) for name in fields if name in dt.names]
>> +    new_dtype = [(name, dt[name]) for name in dt.names if name in fields]
>>     if ary.flags.f_contiguous:
>>         order = 'F'
>>     else:

Nice!  That works for me.

> You can file a ticket, but if this is a function that is already in
> real use, then it would be an unpleasant break in the API
>

Yeah, I would have to change some code around, but I think this would
be a worthwhile enhancement.  Also worth noting that this wasn't
documented anywhere.  I only knew about it because Travis pointed it
out on the list once.

Skipper



More information about the NumPy-Discussion mailing list