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><code>&lt;memory at 0x000000F2F5150348&gt;</code> is a <code>memoryview</code> object located at address <code>0x000000F2F5150348</code>; the buffer it provides access to is located somewhere else.</p>

<p>Memoryviews provide a number of operations described in the <a href="https://docs.python.org/3/library/stdtypes.html#typememoryview" rel="nofollow">relevant official documentation</a>, but at least on the Python-side API, they do not provide any way to access the raw address of the memory they expose. Particularly, the <code>at whatevernumber</code> number is not what you're looking for.</p>

python python-3.x numpy