title: sum over bin range in python
body: <p>I have a numpy array with shape:</p>
  
  <pre><code>In: imar.shape 
  Out: (21, 77, 10000)
  </code></pre>
  
  <p>I want a binned sum on the last axis, with every bin containing 20 items.</p>
  
  <p>The way I'm doing this now is:</p>
  
  <pre><code>np.sum(  imar.reshape([-1,500,20]), axis=2 ).reshape(imar.shape[:2])
  </code></pre>
  
  <p>It's fast, but seems error-prone if I get the arguments to reshape wrong. Is there a better way to do this?</p>
  
  <p>I've looked at np.digitize,histogram,bincount, and some others, but those are value based; I want sum over a set of ranges.</p>