<div dir="ltr"><div class="markdown-here-wrapper" style="font-size:1em;font-family:Helvetica,arial,freesans,clean,sans-serif;color:rgb(34,34,34);background-color:rgb(255,255,255);border:none;line-height:1.2"><p style="margin:1em 0px">Here’s a hack that lets you keep using ==:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;overflow:auto;margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,255)">class IsCompare:
    __array_priority__ = 999999  # needed to make it work on either side of `==`
    def __init__(self, val): self._val = val
    def __eq__(self, other): return other is self._val
    def __neq__(self, other): return other is not self._val

a == IsCompare(None)  # a is None
a == np.array(IsCompare(None)) # broadcasted a is None
</code></pre><p style="margin:1em 0px">Eric</p>
<div title="MDH:SGVyZSdzIGEgaGFjayB0aGF0IGxldHMgeW91IGtlZXAgdXNpbmcgPT06PGRpdj48YnI+PC9kaXY+
PGRpdj5gYGA8L2Rpdj48ZGl2PjxkaXY+Y2xhc3MgSXNDb21wYXJlOjwvZGl2PjxkaXY+Jm5ic3A7
ICZuYnNwOyBfX2FycmF5X3ByaW9yaXR5X18gPSA5OTk5OTkgJm5ic3A7IyBuZWVkZWQgdG8gbWFr
ZSBpdCB3b3JrIG9uIGVpdGhlciBzaWRlIG9mIGA9PWA8L2Rpdj48ZGl2PiZuYnNwOyAmbmJzcDsg
ZGVmIF9faW5pdF9fKHNlbGYsIHZhbCk6IHNlbGYuX3ZhbCA9IHZhbDwvZGl2PjxkaXY+Jm5ic3A7
ICZuYnNwOyBkZWYgX19lcV9fKHNlbGYsIG90aGVyKTogcmV0dXJuIG90aGVyIGlzIHNlbGYuX3Zh
bDwvZGl2PjxkaXY+Jm5ic3A7ICZuYnNwOyBkZWYgX19uZXFfXyhzZWxmLCBvdGhlcik6IHJldHVy
biBvdGhlciBpcyBub3Qgc2VsZi5fdmFsPC9kaXY+PC9kaXY+PGRpdj48YnI+PC9kaXY+PGRpdj5h
ID09IElzQ29tcGFyZShOb25lKSAmbmJzcDsjIGEgaXMgTm9uZTwvZGl2PjxkaXY+YSA9PSBucC5h
cnJheShJc0NvbXBhcmUoTm9uZSkpICMgYnJvYWRjYXN0ZWQgYSBpcyBOb25lPC9kaXY+PGRpdj5g
YGA8L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PkVyaWM8L2Rpdj4=" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0">​</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, 17 Jul 2017 at 17:45 Robert Kern <<a href="mailto:robert.kern@gmail.com">robert.kern@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">On Mon, Jul 17, 2017 at 2:13 AM, <<a href="mailto:Martin.Gfeller@swisscom.com" target="_blank">Martin.Gfeller@swisscom.com</a>> wrote:<br>><br>> Dear all<br>><br>> I have object array of arrays, which I compare element-wise to None in various places:<br>><br>> >>> a = numpy.array([numpy.arange(5),None,numpy.nan,numpy.arange(6),None],dtype=numpy.object)<br>> >>> a<br>> array([array([0, 1, 2, 3, 4]), None, nan, array([0, 1, 2, 3, 4, 5]), None], dtype=object)<br>> >>> numpy.equal(a,None)<br>> FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.<br>><br>><br>> So far, I always ignored the warning, for lack of an idea how to resolve it.<br>><br>> Now, with Numpy 1.13, I have to resolve the issue, because it fails with:<br>><br>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()<br>><br>> It seem that the numpy.equal is applied to each inner array, returning a Boolean array for each element, which cannot be coerced to a single Boolean.<br>><br>> The expression<br>><br>> >>> numpy.vectorize(operator.is_)(a,None)<br>><br>> gives the desired result, but feels a bit clumsy.<br><br></div><div dir="ltr">Wrap the clumsiness up in a documented, tested utility function with a descriptive name and use that function everywhere instead.<br><br>--<br>Robert Kern</div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>