[Numpy-discussion] How to compare an array of arrays elementwise to None in Numpy 1.13 (was easy before)?
sebastian at sipsolutions.net
Mon Jul 17 05:41:28 EDT 2017
On Mon, 2017-07-17 at 09:13 +0000, Martin.Gfeller at swisscom.com wrote:
> Dear all
> I have object array of arrays, which I compare element-wise to None
> in various places:
> > > > a =
> > > > numpy.array([numpy.arange(5),None,numpy.nan,numpy.arange(6),Non
> > > > e],dtype=numpy.object)
> > > > a
> array([array([0, 1, 2, 3, 4]), None, nan, array([0, 1, 2, 3, 4, 5]),
> None], dtype=object)
> > > > numpy.equal(a,None)
> FutureWarning: comparison to `None` will result in an elementwise
> object comparison in the future.
> So far, I always ignored the warning, for lack of an idea how to
> resolve it.
> Now, with Numpy 1.13, I have to resolve the issue, because it fails
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
> 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.
> The expression
> > > > numpy.vectorize(operator.is_)(a,None)
> gives the desired result, but feels a bit clumsy.
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.
> Is there a cleaner, efficient way to do an element-wise (but shallow)
> Thank you and best regards,
> Martin Gfeller, Swisscom
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 801 bytes
Desc: This is a digitally signed message part
More information about the NumPy-Discussion