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

Ralf Gommers ralf.gommers@googlemail....
Thu Mar 10 01:23:59 CST 2011


On Thu, Mar 10, 2011 at 9:37 AM, Skipper Seabold <jsseabold@gmail.com> wrote:
> On Wed, Mar 9, 2011 at 8:13 PM, Ralf Gommers
> <ralf.gommers@googlemail.com> wrote:
>> On Wed, Mar 9, 2011 at 11:24 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
>>>>>
>>>>> 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.
>>
>
> I assume this is the same thing

Taking a view is explicitly documented as "reinterpreting bytes in
memory". Casting on assignment to a structured array should have
nothing to do with that (says my common sense, not sure what's
actually happening).

Ralf


> x = np.array([1.5])
> x.view(int)
> array([4609434218613702656])
>
> x = np.array([1])
> x.view(float)
> array([  4.94065646e-324])
>
> I've been bit by this before, so I know not to do it, but I think it
> would be nice if it threw up a "don't try do that!".
>
> Skipper
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list