title: simpler way to filter out nan or invalid's in Python array or a numpy array?
body: <p>I have numpy array <code>heights</code> which may have <code>nan</code>'s in it. I clean it by doing:</p>
  
  <pre><code>heights = numpy.asarray([ h for h in heights if not numpy.isnan(h) ])
  </code></pre>
  
  <p>This seems like a rather verbose way to express such a simple/common thing. I often have to do this as well for filtering my arrays in other ways and have to fall back on array building, which works but I bet there is a better way to do it. E.g. filtering by range...</p>
  
  <pre><code>heights = numpy.asarray(heights[lowerBound &lt;= heights &amp; heights &lt; upperBound])
  </code></pre>
  
  <p>fails <em>in python</em>, where heights is still a numpy array. I fall back to doing...</p>
  
  <p>EDIT: The error message for this line is: </p>
  
  <p><em>TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''</em></p>
  
  <p>/EDIT</p>
  
  <pre><code>heights = numpy.asarray(heights[[h for h in heights if lowerBound &lt;= h and h &lt; upperBound]])
  </code></pre>
  
  <p>Gross. I've been using python for a 2-3 months now, but I'm still not really getting how to effectively and simply use the numpy masking system. I came from heavily using matlab, where a "mask" would be an array of booleans of the same shape/size. E.g.</p>
  
  <pre><code>heights = heights(~isnan(heights));
  </code></pre>
  
  <p>or alternatively...</p>
  
  <pre><code>heights(isnan(heights)) = [];
  </code></pre>
  
  <p>Both of these look super clean. Also, the bounds example that fails in python works in matlab, although the brackets must be changed to parentheses...</p>
  
  <pre><code>heights = heights(lowerBound &lt;= heights &amp; heights &lt; upperBound)
  </code></pre>
  
  <p>How can i write these simple data transformations similarly elegantly in python/numpy, pythonic or otherwise?</p>