[Numpy-discussion] How to compare an array of arrays elementwise to None in

Martin.Gfeller at swisscom.com Martin.Gfeller at swisscom.com
Wed Jul 19 04:31:34 EDT 2017

Thank you for your help!

Sebastian, I couldn't agree more with someone's bug being someone else's feature! A fast identity ufunc would be useful, though. 

Actually, numpy.frompyfunc(operator.is_,2,1) is much faster than the numpy.vectorize approach - only about 35% slower on quick measurement 
than the direct ==, as opposed to 62% slower with vectorize (with otypes hint). 

Robert, yes, that's what I already did provisionally. 

Eric, that is a nice puzzle - but I agree with Robert about understanding by code maintainers. 

Thanks again, and best regards,

On Mon, 17 Jul 2017 11:41 Sebastian Berg <sebastian at sipsolutions.net> write

> Yes, I guess ones bug is someone elses feature :(, if it is very bad, we could delay the deprecation probably. For a solutions, maybe 
> we could add a ufunc  for elementwise `is` on object arrays (dunno about the name, maybe `object_identity`.

> Just some quick thoughts.

> - Sebastian

On Mon, 17 Jul 2017 at 17:45 Robert Kern <robert.kern at gmail.com> wrote:

> Wrap the clumsiness up in a documented, tested utility function with a descriptive name and use that function everywhere instead.
> Robert Kern

On Mon, Jul 17, 2017 at 10:52 AM, Eric Wieser <wieser.eric+numpy at gmail.com>

> Here's a hack that lets you keep using ==:
> 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

More information about the NumPy-Discussion mailing list