Behavior of ndarray.data for views in numpy
<p>I am trying to understand the meaning of <code>ndarray.data</code> field in numpy (see <a href="http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#memory-layout" rel="nofollow">memory layout</a> section of the reference page on N-dimensional arrays), especially for views into arrays. To quote the documentation:</p>

<blockquote>
  <p>ndarray.data -- Python buffer object pointing to the start of the arrayâs data</p>
</blockquote>

<p>According to this description, I was expecting this to be a pointer to the C-array underlying the instance of ndarray.</p>

<p>Consider <code>x = np.arange(5, dtype=np.float64)</code>. </p>

<p>Form <code>y</code> as a view into <code>x</code> using a slice: <code>y = x[3:1:-1]</code>. </p>

<p>I was expecting <code>x.data</code> to point at location of <code>0.</code> and <code>y.data</code> to point at the location of <code>3.</code>. I was expecting the memory pointer printed by <code>y.data</code> to thus be offset by <code>3*x.itemsize</code> bytes from the memory pointer printed by <code>x.data</code>, but this does not appear to be the case:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; x = np.arange(5, dtype=np.float64)
&gt;&gt;&gt; y = x[ 3:1:-1]
&gt;&gt;&gt; x.data
&lt;memory at 0x000000F2F5150348&gt;
&gt;&gt;&gt; y.data
&lt;memory at 0x000000F2F5150408&gt;
&gt;&gt;&gt; int('0x000000F2F5150408', 16) - int('0x000000F2F5150348', 16)
192
&gt;&gt;&gt; 3*x.itemsize
24
</code></pre>

<p>The <code>'data'</code> key in <code>__array_interface</code> dictionary associated with the ndarray instance behaves more like I expect, although it may itself not be a pointer:</p>

<pre><code>&gt;&gt;&gt; y.__array_interface__['data'][0] - x.__array_interface__['data'][0]
24
</code></pre>

<blockquote>
  <p>So this begs the question, what does the <code>ndarray.data</code> give? </p>
</blockquote>

<p>Thanks in advance.</p>

<p>Generally the number displayed by <code>x.data</code> isn't meant to be used by you.  <code>x.data</code> is the buffer, which can be used in other contexts that expect a buffer.</p>

<pre><code>np.frombuffer(x.data,dtype=float)
</code></pre>

<p>replicates your <code>x</code>. </p>

<pre><code>np.frombuffer(x[3:].data,dtype=float)
</code></pre>

<p>this replicates <code>x[3:]</code>.  But from Python you can't take <code>x.data</code>, add 192 bits (3*8*8) to it, and expect to get <code>x[3:]</code>.</p>

<p>I often use the <code>__array_interface__['data']</code> value to check whether two variables share a data buffer, but I don't use that number for any thing.  These are informative numbers, not working values.</p>

<p>I recently explored this in</p>

<p><a href="http://stackoverflow.com/questions/39376892/creating-a-numpy-array-directly-from-array-interface/39377877#39377877">Creating a NumPy array directly from __array_interface__</a></p>

python python-3.x numpy