Pythonic way to access the shifted version of numpy array?
<p>What is a pythonic way to access the shifted, either right or left, of a <code>numpy</code> array? A clear example:</p>

<pre><code>a = np.array([1.0, 2.0, 3.0, 4.0])
</code></pre>

<p>Is there away to access:</p>

<pre><code>a_shifted_1_left = np.array([2.0, 3.0, 4.0, 1.0])
</code></pre>

<p>from the <code>numpy</code> library?</p>

<p>You are looking for <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.roll.html" rel="nofollow"><code>np.roll</code></a> -</p>

<pre><code>np.roll(a,-1) # shifted left
np.roll(a,1) # shifted right
</code></pre>

<p>Sample run -</p>

<pre><code>In [28]: a
Out[28]: array([ 1.,  2.,  3.,  4.])

In [29]: np.roll(a,-1) # shifted left
Out[29]: array([ 2.,  3.,  4.,  1.])

In [30]: np.roll(a,1) # shifted right
Out[30]: array([ 4.,  1.,  2.,  3.])
</code></pre>

<p>If you want more shifts, just go <code>np.roll(a,-2)</code> and <code>np.roll(a,2)</code> and so on.</p>

python arrays numpy