[Numpy-discussion] dtype "reduction"

Nathaniel Smith njs@pobox....
Wed Dec 26 18:32:37 CST 2012


On Wed, Dec 26, 2012 at 8:09 PM, Nicolas Rougier
<Nicolas.Rougier@inria.fr> wrote:
>
>
> Hi all,
>
>
> I'm looking for a way to "reduce" dtype1 into dtype2 (when it is possible of course).
> Is there some easy way to do that by any chance ?
>
>
> dtype1 = np.dtype( [ ('vertex',  [('x', 'f4'),
>                                   ('y', 'f4'),
>                                   ('z', 'f4')]),
>                     ('normal',  [('x', 'f4'),
>                                  ('y', 'f4'),
>                                  ('z', 'f4')]),
>                     ('color',   [('r', 'f4'),
>                                  ('g', 'f4'),
>                                  ('b', 'f4'),
>                                  ('a', 'f4')]) ] )
>
> dtype2 = np.dtype( [ ('vertex',  'f4', 3),
>                      ('normal',  'f4', 3),
>                      ('color',   'f4', 4)] )
>

If you have an array whose dtype is dtype1, and you want to convert it
into an array with dtype2, then you just do
  my_dtype2_array = my_dtype1_array.view(dtype2)

If you have dtype1 and you want to programmaticaly construct dtype2,
then that's a little more fiddly and depends on what exactly you're
trying to do, but start by poking around with dtype1.names and
dtype1.fields, which contain information on how dtype1 is put together
in the form of regular python structures.

-n


More information about the NumPy-Discussion mailing list