[Numpy-discussion] Truth value of ndarray not Pythonic

Jim Kitchen jim22k at gmail.com
Thu Nov 29 11:24:56 EST 2012

I understand the historical reason for the "Truth value of an array" error,
avoiding the pitfalls of:
>>> a = np.arange(10)
>>> if a==0: # this is ambiguous, use any() or all()

I also understand the issues with logical and/or:
>>> if (a<10 and a > 5): # this will break due to "and" vs "&"

However the main point in this thread from 3 years
very valid.  If I write code that uses lists and then convert that to
array for efficiency or more powerful computation, I have my own pitfall
trying to do:
>>> if a: # why doesn't this just check for size?

My Proposal
It seems to me that an elegant solution to this dilemma is to separate the
behavior of ndarrays of type bool from all other ndarrays.  Keep the
current behavior for ndarrays of type bool, but let the __nonzero__ for all
other ndarrays be based on size.

>>> if a==0: # still raises Truth error because it's of dtype bool

>>> if (a<10 and a>5): # still raises Truth error because it's of dtype bool

>>> if a: # works fine because dtype is int64

This solution avoids all the primary pitfalls of ambiguity where someone
would need any() or all() because they're working with bools at the element
level.  But for cases where a function may return data or None, I really
like to use the normal Python truth test for that instead of:
>>> if a is not None and len(a) > 0: # that was a chore to find out

The only problem I see with this solution is with the case of the
single-element array.
>>> s = np.array([[0]])
>>> if s: # works today, returns False

With my proposal,
>>> if s: # still works, but returns True because array is not empty

It's a wart to be sure, but it would make ndarrays much easier to work with
when converting from standard Python containers.  Maybe we need something
like this (probably not possible):
>>> from numpy.__future__ import truthiness

I've especially found this Truth error a challenge converting from
dictionaries of lists to pandas DataFrames.  It raises the same error,
tracing back to this ambiguity in ndarrays.  If it's too big of a change to
make for ndarrays, maybe the same proposal could be implemented in pandas.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121129/fbee9515/attachment.html>

More information about the NumPy-Discussion mailing list