[Numpy-discussion] Behavior from a change in dtype?

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Sep 7 21:08:44 EDT 2009


On Mon, Sep 7, 2009 at 8:01 PM, Skipper Seabold<jsseabold at gmail.com> wrote:
> On Mon, Sep 7, 2009 at 7:35 PM, <josef.pktd at gmail.com> wrote:
>> On Mon, Sep 7, 2009 at 6:36 PM, Skipper Seabold<jsseabold at gmail.com> wrote:
>>> Hello all,
>>>
>>> I ran into a problem with some of my older code (since figured out the
>>> user error).  However, in trying to give a simple example that
>>> replicates the problem I was having, I ran into this.
>>>
>>> In [19]: a = np.array((1.))
>>>
>>> In [20]: a
>>> Out[20]: array(1.0)
>>>
>>> # the dtype is 'float64'
>>>
>>> In [21]: a.dtype='<i8'
>>
>> The way I understand it is:
>> Here you are telling numpy to interpret the existing memory/data in a
>> different way, which might make sense or not depending on the types,
>> e.g. I also used this to switch between structured arrays and regular
>> arrays with compatible memory. However it does not convert the data.
>>
>> If you want to convert the data to a different type, numpy needs to
>> create a new array, e.g. with astype
>>
>>>>> a = np.array((1.))
>>>>> b = a.astype('<i8')
>>>>> b
>> array(1L, dtype=int64)
>>
>> Josef
>>
>
> Hmm, okay, well I came across this in trying to create a recarray like
> data2 below, so I guess I should just combine the two questions.  Is
> the last example the best way to do what I'm trying to do (taken from
> an old thread)?  I would like to add a few more examples of best
> practice here <http://docs.scipy.org/doc/numpy/user/basics.rec.html>,
> so I don't need to go looking again.
>
> import numpy as np
>
> data = np.array([[10.75, 1, 1],[10.39, 0, 1],[18.18, 0, 1]])
> dt = np.dtype([('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])
> data2 = data.copy()
> data3 = data.copy()
>
> # Doesn't work, raises TypeError: expected a readable buffer object
> data2 = data2.view(np.recarray)
> data2.astype(dt)
>
> # Works without error (?) with unexpected result
> data3 = data3.view(np.recarray)
> data3.dtype = dt
>
> # One correct (though IMHO) unintuitive way
> data = np.rec.fromarrays(data.swapaxes(1,0), dtype=dt)


I'm not able to come up with anything much better.
For the conversion to structured arrays, numpy seems to expect tuples
for the rows:

>>> data = np.array([[10.75, 1, 1],[10.39, 0, 1],[18.18, 0, 1]])
>>> np.array(map(tuple, data), dt)
array([(10.75, 1L, 1L), (10.390000000000001, 0L, 1L), (18.18, 0L, 1L)],
      dtype=[('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])
>>> np.array(map(tuple, data), dt).view(np.recarray)
rec.array([(10.75, 1L, 1L), (10.390000000000001, 0L, 1L), (18.18, 0L, 1L)],
      dtype=[('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])


One way of doing it is to create a float view first, as below.
But I would also like to know if there is a better way, I still find
the conversion
between structured and regular arrays for non-homogenous unintuitive.

Josef


>>> data = np.array([[10.75, 1, 1],[10.39, 0, 1],[18.18, 0, 1]])
>>> data.dtype
dtype('float64')
>>> dt = np.dtype([('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])
>>> data.astype(dt)
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    data.astype(dt)
TypeError: expected a readable buffer object
>>> # doh
>>> data_stf = data.view([('',data.dtype)]*data.shape[1])
>>> data_stf.dtype
dtype([('f0', '<f8'), ('f1', '<f8'), ('f2', '<f8')])
>>> data_stf.astype(dt)
array([[(10.75, 1L, 1L)],
       [(10.390000000000001, 0L, 1L)],
       [(18.18, 0L, 1L)]],
      dtype=[('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])
>>> data_stf.astype(dt).view(np.recarray)
rec.array([[(10.75, 1L, 1L)],
       [(10.390000000000001, 0L, 1L)],
       [(18.18, 0L, 1L)]],
      dtype=[('var1', '<f8'), ('var2', '<i8'), ('var3', '<i8')])
>>> # Yuhoo


>
> Skipper
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list