title: Wrap slice around edges of a 2D array in numpy
body: <p>Suppose I am working with numpy in Python and I have a two-dimensional array of  arbitrary size. For convenience, let's say I have a 5 x 5 array. The specific numbers are not particularly important to my question; they're just an example.</p>
  
  <pre><code>a = numpy.arrange(25).reshape(5,5)
  </code></pre>
  
  <p>This yields:</p>
  
  <pre><code>[[0, 1, 2, 3, 4 ],
   [5, 6, 7, 8, 9 ],
   [10,11,12,13,14],
   [15,16,17,18,19],
   [20,21,22,23,24]]
  </code></pre>
  
  <p>Now, let's say I wanted to take a 2D slice of this array. In normal conditions, this would be easy. To get the cells immediately adjacent to 2,2 I would simply use <code>a[1:4,1,4]</code> which would yield the expected</p>
  
  <pre><code>[[6, 7,   8 ],
   [11, 12, 13],
   [16, 17, 18]]
  </code></pre>
  
  <p>But what if I want to take a slice that wraps 
  around the edges of the array? For example <code>a[-1:2,-1:2]</code> would yield:</p>
  
  <pre><code>[24, 20, 21],
  [4, 0,  1 ],
  [9, 5,  6 ] 
  </code></pre>
  
  <p>This would be useful in several situations where the edges don't matter, for example game graphics that wrap around a screen. I realize this can be done with a lot of if statements and bounds-checking, but I was wondering if there was a cleaner, more idiomatic way to accomplish this. </p>
  
  <p>Looking around, I have found several answers such as this: <a href="https://stackoverflow.com/questions/17739543/wrapping-around-slices-in-python-numpy">https://stackoverflow.com/questions/17739543/wrapping-around-slices-in-python-numpy</a> that work for 1-dimensional arrays, but I have yet to figure out how to apply this logic to a 2D slice.</p>
  
  <p>So essentially, the question is: how do I take a 2D slice of a 2D array in numpy that wraps around the edges of the array?  </p>
  
  <p>Thank you in advance to anyone who can help.</p>