[Numpy-discussion] Multiple-field indexing: view vs copy in 1.14+
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.
>>>> a = np.ones(5, dtype=[('a', 'i8'), ('b', 'f8'), ('c', 'f8')])
> 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
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.
More information about the NumPy-Discussion