boolean indexing in xarray
<p>I have some arrays with dims <code>'time', 'lat', 'lon'</code> and some with just <code>'lat', 'lon'</code>. I often have to do this in order to mask time-dependent data with a 2d (lat-lon) mask:</p>

<pre><code>x.data[:, mask.data] = np.nan
</code></pre>

<p>Of course, computations broadcast as expected. If <code>y</code> is 2d lat-lon data, its values are broadcast to all time coordinates in x:</p>

<pre><code>z = x + y
</code></pre>

<p>But indexing doesn't broadcast as I'd expect. I'd like to be able to do this, but it raises <em>ValueError: Buffer has wrong number of dimensions</em>:</p>

<pre><code>x[mask] = np.nan
</code></pre>

<p>Lastly, it seems that <code>xr.where</code> <em>does</em> broadcast the values of the mask across time coordinates as expected, but you can't set values this way.</p>

<pre><code>x_masked = x.where(mask)
</code></pre>

<p>So, is there something I'm missing here that facilitates setting values using a boolean mask that is missing dimensions (and needs to be broadcast)? Is the option I provided at the top really the way to do this (in which case, I might as well just be using standard numpy arrays...)</p>

<p>Somewhat related question here: <a href="http://stackoverflow.com/questions/38884283/concise-way-to-filter-data-in-xarray">Concise way to filter data in xarray</a></p>

<p>Currently the best approach is a combination of <code>.where</code> and <code>.fillna</code>. </p>

<pre><code>valid = date_by_items.notnull()
positive = date_by_items &gt; 0
positive = positive * 2
result = positive.fillna(0.).where(valid)
result
</code></pre>

<p>But changes are coming in xarray that will make this more concise - checkout the GitHub repo if you're interested</p>

python numpy python-xarray