<div dir="ltr">Hi,<div><br></div><div>The numpy.where() function was rewritten in numpy 1.9 to speed it up.  I traced it to this changeset.  <a href="https://github.com/numpy/numpy/commit/593e3c30c24f0c61a271dc883c614724d7a57e1e">https://github.com/numpy/numpy/commit/593e3c30c24f0c61a271dc883c614724d7a57e1e</a></div><div><br></div><div>The weird thing is the 1.9 behavior changed the resulting dtype in some situations when using scalar values as the second or third argument.  To try and illustrate, I wrote a simple test script and ran it against both numpy 1.7 and 1.9.  Here are the results:</div><div><br></div><div><div>2.7.9 (default, Jul 25 2015, 03:06:43) </div><div>[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]</div><div>***** numpy version 1.7.2 *****</div><div><br></div><div>=== testing numpy.where with NaNs ===</div><div>numpy.where([True], numpy.float32(1.0), numpy.NaN).dtype</div><div>float64</div><div>numpy.where([True], [numpy.float32(1.0)], numpy.NaN).dtype</div><div>float32</div><div>numpy.where([True], numpy.float32(1.0), [numpy.NaN]).dtype</div><div>float64</div><div>numpy.where([True], [numpy.float32(1.0)], [numpy.NaN]).dtype</div><div>float64</div><div><br></div><div><br></div><div>=== testing numpy.where with integers ===</div><div>numpy.where([True], [numpy.float32(1.0)], 65535).dtype</div><div>float32</div><div>numpy.where([True], [numpy.float32(1.0)], 65536).dtype</div><div>float32</div><div>numpy.where([True], [numpy.float32(1.0)], -32768).dtype</div><div>float32</div><div>numpy.where([True], [numpy.float32(1.0)], -32769).dtype</div><div>float32</div></div><div><br></div><div><br></div><div><br></div><div><div>2.7.9 (default, Mar 10 2015, 09:26:44) </div><div>[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]</div><div>***** numpy version 1.9.2 *****</div><div><br></div><div>=== testing numpy.where with NaNs ===</div><div>numpy.where([True], numpy.float32(1.0), numpy.NaN).dtype</div><div>float64</div><div>numpy.where([True], [numpy.float32(1.0)], numpy.NaN).dtype</div><div>float32</div><div>numpy.where([True], numpy.float32(1.0), [numpy.NaN]).dtype</div><div>float64</div><div>numpy.where([True], [numpy.float32(1.0)], [numpy.NaN]).dtype</div><div>float64</div><div><br></div><div><br></div><div>=== testing numpy.where with integers ===</div><div>numpy.where([True], [numpy.float32(1.0)], 65535).dtype</div><div>float32</div><div>numpy.where([True], [numpy.float32(1.0)], 65536).dtype</div><div>float64</div><div>numpy.where([True], [numpy.float32(1.0)], -32768).dtype</div><div>float32</div><div>numpy.where([True], [numpy.float32(1.0)], -32769).dtype</div><div>float64</div><div><br></div></div><div><br></div><div><br></div><div>Regarding the NaNs with where, the behavior does not differ between 1.7 and 1.9.  But it's a little odd that the one scenario returns a dtype of float32 where the other three scenarios return dtype of float64.  I'm not sure if that was intentional or a bug?</div><div><br></div><div>Regarding using ints with where, in 1.7 the resulting dtype is consistent but then in 1.9 the resulting dtype is influenced by the value of the int.  It appears it is somehow related to whether the value falls within the range of a short.  I'm not sure if this was a side effect of the performance improvement or was intentional?</div><div><br></div><div>At the very least I think this change in where() should probably be noted in the release notes for 1.9.  Our project saw an increase in memory usage with 1.9 due to where(cond, array, scalar) returning arrays of dtype float64 when using scalars not within that limited range.</div><div><br></div><div>I've attached my simple script if you're interested in running it.</div></div>