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

Stefan van der Walt stefanv at berkeley.edu
Thu Jan 25 13:16:31 EST 2018


On Mon, 22 Jan 2018 10:11:08 -0500, Marten van Kerkwijk wrote:
>I think on the consistency argument is perhaps the most important:
>views are very powerful and in many ways one *counts* on them
>happening, especially in working with large arrays.

I had the same gut feeling, but the fancy indexing example made me
pause:

In [9]: x = np.arange(12, dtype=float).reshape((3, 4))

In [10]: p = x[[0, 1]]  # copy of data

Then:

In [11]: x = np.array([(0, 1), (2, 3)], dtype=[('a', int), ('b', int)])

In [12]: p = x[['a', 'b']]  # copy of data, but proposal will change that

We're not doing the same kind of indexing here exactly (in one case we
grab elements, in the other parts of elements), but the view behavior
may still break the "mental expectation".

Fortunately, there's already other proof that this operatoin is not
exactly fancy indexing:

In [15]: x[['a', 'a']]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-15-3629f1d5c01d> in <module>()
----> 1 x[['a', 'a']]

KeyError: 'duplicate field of name a'


Not copying wherever possible feels like an important principle to
uphold, so I am +1.

Stéfan


More information about the NumPy-Discussion mailing list