[Numpy-discussion] printing structured arrays

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 9 11:50:41 CST 2010


On Mon, Mar 8, 2010 at 5:50 PM, Tim Michelsen
<timmichelsen@gmx-topmail.de> wrote:
> Hello,
> thanks to all who responded and have their input here.
>
> I added a little code snippet to show the view and reshape:
>
> http://www.scipy.org/Cookbook/Recarray
>
> What do you think?
> Is this worth to go into the official docs?
> The page http://docs.scipy.org/doc/numpy/user/basics.rec.html is quite
> sparse...
>
> I still wonder why there is not a quick function for such a view /
> reshape conversion.


Thanks, the docs for working with arrays with structured dtypes are sparse

Note that in your example .view(np.ndarray) doesn't do anything

>>> struct_diffdtype[['str_var', 'x', 'y']].view(np.ndarray).reshape(len(struct_diffdtype),-1)
array([[(1.0, 'string1', 2.0)],
       [(3.0, 'string2', 4.0999999999999996)]],
      dtype=[('x', '<f8'), ('str_var', '|S7'), ('y', '<f8')])

>>> struct_diffdtype[['x', 'y']].view(np.ndarray).reshape(len(struct_diffdtype),-1)
array([[(1.0, 2.0)],
       [(3.0, 4.0999999999999996)]],
      dtype=[('x', '<f8'), ('y', '<f8')])


view on columns with floating values  (is this a copy???)

>>> struct_diffdtype[['x', 'y']].view(float).reshape(len(struct_diffdtype),-1)
array([[ 1. ,  2. ],
       [ 3. ,  4.1]])

and float view on strings is not possible

>>> struct_diffdtype[['str_var', 'x', 'y']].view(float).reshape(len(struct_diffdtype),-1)
Traceback (most recent call last):
ValueError: new type not compatible with array.

Josef

>
>
> Best regards,
> Timmie
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list