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

Ralf Gommers ralf.gommers@googlemail....
Wed Mar 9 19:13:54 CST 2011


On Wed, Mar 9, 2011 at 11:24 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
> On Tue, Mar 8, 2011 at 8:08 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
>> On Sun, Mar 6, 2011 at 11:12 PM, Ralf Gommers
>> <ralf.gommers@googlemail.com> wrote:
>>> On Sun, Mar 6, 2011 at 1:10 AM, Skipper Seabold <jsseabold@gmail.com> wrote:
>>>> On Sat, Mar 5, 2011 at 9:28 AM, Ralf Gommers
>>>> <ralf.gommers@googlemail.com> wrote:
>>>>> On Sat, Mar 5, 2011 at 8:09 AM, Russell E. Owen <rowen@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.
>>>>
>>>> http://docs.scipy.org/numpy/docs/numpy.doc.structured_arrays/
>>>
>>> Thanks, I'll make sure that goes in for 1.6.0.
>>>
>>>>> 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.
>>>>
>>>> http://thread.gmane.org/gmane.comp.python.numeric.general/30793/focus=30793
>>>>
>>>> Perhaps an enhancement ticket could be filed? It doesn't sound trivial
>>>> to implement.
>>>
>>> I filed #1758.
>>>
>>> You can also assign with an array which fails silently, certainly a bug:
>>>
>>>>>> arr = np.zeros((5,), dtype=[('var1','f8'),('var2','f8')])
>>>>>> arr['var1'] = np.arange(5)
>>>>>> arr[0] = (10,20)
>>>>>> arr[0]
>>> (10.0, 20.0)
>>>
>>>>>> arr[0] = np.array([10,20])  # no exception, but garbage out
>>>>>> arr[0]
>>> (4.2439915824246103e-313, 0.0)
>>>
>>
>> This is a casting issue. Your array is an integer array. You can
>> assign with an array.
>>
>> arr = np.zeros((5,), dtype=[('var1','f8'),('var2','f8')])
>> arr[0] = np.array([10.0,20])
>> arr[0]
>> (10.0, 20.0)
>>
>
> FYI, I fixed the docs to reflect this.

Thanks, the doc is accurate now. Although I'm not sure we want to
document a bug (which I'm still sure it is) like that without making
clear it it is a bug.

> I know numpy is already pretty verbose by default, but should the
> integer case throw up a warning similar to casting from complex to
> real?

Please no, that will be very annoying. Plus it's much better defined
then complex -> real.

Cheers,
Ralf

>>>> x = np.zeros(2)
>>>> x[:] = np.array([1+1j,1+1j])
> ComplexWarning: Casting complex values to real discards the imaginary part
>
> Skipper


More information about the NumPy-Discussion mailing list