<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 30, 2014 at 3:29 PM, Alexander Belopolsky <span dir="ltr"><<a href="mailto:ndarray@mac.com" target="_blank">ndarray@mac.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra" style="font-size:12.5714px"><span><div class="gmail_quote">On Tue, Dec 30, 2014 at 2:49 PM, Benjamin Root <span dir="ltr"><<a href="mailto:ben.root@ou.edu" target="_blank">ben.root@ou.edu</a>></span> wrote:<span class=""><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Where does it say that operations on masked arrays should not produce NaNs?</blockquote></span></div><br></span>Masked arrays were invented with the specific goal to avoid carrying NaNs in computations.  Back in the days, NaNs were not available on some platforms and had significant performance issues on others.  These days NaN support for floating point types is nearly universal, but numpy types are not limited by floating point.  </div><span class=""><span style="font-size:12.5714px"><div class="gmail_extra"><br></div></span></span></div></blockquote><div><br></div><div>From the <a href="http://numpy.ma">numpy.ma</a> docstring:<br>"Arrays sometimes contain invalid or missing data.  When doing operations<br>    on such arrays, we wish to suppress invalid values, which is the purpose masked<br>    arrays fulfill (an example of typical use is given below)."<br><br></div><div>A few lines down:<br>"Here, we construct a masked array that suppress all ``NaN`` values.  We<br>    may now proceed to calculate the mean of the other values"<br><br></div><div>Note the repeated usage of the term "suppress" in the context of the input arrays. The phrase "We may now proceed to calculate the mean of the other values" implies that the mean of a masked array is taken to be the mean of everything but the masked values. If there are no values remaining, then I expect it to give me the equivalent of np.mean([]).<br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class=""><span style="font-size:12.5714px"><div class="gmail_extra"></div><div class="gmail_extra">> <span style="font-size:12.5714px">Having np.mean([]) return the same thing as np.ma.mean([]) makes complete sense.</span></div><div class="gmail_extra"><span style="font-size:12.5714px"><br></span></div></span></span><div class="gmail_extra" style="font-size:12.5714px"><span style="font-size:12.5714px">Does the following make sense as well?</span></div><div class="gmail_extra" style="font-size:12.5714px"><span style="font-size:12.5714px"><br></span></div><div class="gmail_extra" style="font-size:12.5714px"><span style="font-size:12.5714px"><div class="gmail_extra">>>> import numpy</div><div class="gmail_extra">>>> numpy.ma.masked_values([0, 0], 0).mean()</div><div class="gmail_extra">masked</div><div class="gmail_extra">>>> numpy.ma.masked_values([0], 0).mean()</div><div class="gmail_extra">masked</div><div class="gmail_extra">>>> numpy.ma.masked_values([], 0).mean()</div><div class="gmail_extra">* Two warnings *</div><span class=""><span><div class="gmail_extra">masked_array(data = nan,</div><div class="gmail_extra">             mask = False,</div></span></span><div class="gmail_extra">       fill_value = 0.0)</div></span></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div></div></blockquote><div><br>No, I would consider the first two to be bugs. And actually, returning a masked array in the third one is also incorrect in this case. The result should be a scalar. This is now veering to the same issues discussed in the np.nanmean([]) vs. np.nanmean([np.nan]) discussion.<br><br></div><div>Cheers!<br></div><div>Ben Root<br></div></div></div></div>