View on sliced array without copy
Hi, I'm trying to get a view on a sliced array without copy but I'm not able to do it as illustrated below: dtype = np.dtype( [('color', 'f4', 4)] ) Z = np.zeros(100, dtype=dtype) print Z.view('f4').base is Z # True print Z[:50].base is Z # True print (Z.view('f4'))[:50].base is Z # False print Z[:50].view('f4').base is Z # False Did I do something wrong or is it expected behavior ? Nicolas
On Tue, Feb 12, 2013 at 9:53 AM, Nicolas Rougier <Nicolas.Rougier@inria.fr>wrote:
Did I do something wrong or is it expected behavior ?
Try: print (Z.view('f4'))[:50].base.base is Z # True print Z[:50].view('f4').base.base is Z # True This weird behaviour is fixed in the justreleased numpy 1.7. From the notes of the release: The ``.base`` attribute on ndarrays, which is used on views to ensure that the underlying array owning the memory is not deallocated prematurely, now collapses out references when you have a viewofaview. For example:: a = np.arange(10) b = a[1:] c = b[1:] In numpy 1.6, ``c.base`` is ``b``, and ``c.base.base`` is ``a``. In numpy 1.7, ``c.base`` is ``a``. Jaime  (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
I did not know that. Thanks for the clear explanation. Nicolas On Feb 12, 2013, at 19:25 , Jaime Fernández del Río wrote:
On Tue, Feb 12, 2013 at 9:53 AM, Nicolas Rougier <Nicolas.Rougier@inria.fr> wrote: Did I do something wrong or is it expected behavior ?
Try:
print (Z.view('f4'))[:50].base.base is Z # True print Z[:50].view('f4').base.base is Z # True
This weird behaviour is fixed in the justreleased numpy 1.7. From the notes of the release:
The ``.base`` attribute on ndarrays, which is used on views to ensure that the underlying array owning the memory is not deallocated prematurely, now collapses out references when you have a viewofaview. For example::
a = np.arange(10) b = a[1:] c = b[1:]
In numpy 1.6, ``c.base`` is ``b``, and ``c.base.base`` is ``a``. In numpy 1.7, ``c.base`` is ``a``.
Jaime
 (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial. _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
participants (2)

Jaime Fernández del Río

Nicolas Rougier