[Numpy-discussion] Multiple-field indexing: view vs copy in 1.14+

Allan Haldane allanhaldane at gmail.com
Mon Jan 22 11:23:28 EST 2018


On 01/22/2018 10:53 AM, josef.pktd at gmail.com wrote:
> On Sun, Jan 21, 2018 at 9:48 PM, Allan Haldane <allanhaldane at gmail.com 
> 
> In  many examples where I used structured dtypes a long time ago, 
> switched between consistent views as either a standard array of subsets 
> or as .structured dtypes.
> For this usecase it wouldn't matter whether a[['a', 'c']] returns a view 
> or copy, as long as we can get the second view that is consistent with 
> the selected part of the memory. This would also be independent of 
> whether numpy pads internally and adjusts the strides if possible or not.
> 
>>>> np.__version__
> '1.11.2'
> 
>>>> a = np.ones(5, dtype=[('a', 'i8'), ('b', 'f8'), ('c', 'f8')])
>>>> a
> array([(1, 1.0, 1.0), (1, 1.0, 1.0), (1, 1.0, 1.0), (1, 1.0, 1.0),
>         (1, 1.0, 1.0)],
>        dtype=[('a', '<i8'), ('b', '<f8'), ('c', '<f8')])
>>>> a[['b', 'c']].view(('f8', 2)).dtype
> dtype('float64')

Thanks for a real example to think about. I just want to note that I 
thought of another way to "fix" this for 1.15 which does not involve 
"pack_fields", which is

     a[['b', 'c']].astype('f8,f8').view(('f8', 2))

Which is back-compatible will numpy back to 1.7, I think.

So that's another option to ease the transition.

Allan


More information about the NumPy-Discussion mailing list