any() for object-dtype arrays returns non-booleans
See original GitHub issueThe any
function seems to not deal well for arrays of dtype object
. According to the documentation it should return a boolean. However, consider the following code:
print(numpy.array([5, None]).any())
To my surprise, this prints 5
, rather than True
. It seems to always return the first element of which the boolean value is True
, or, if no such elements exist, the last element. (This means that numpy.array([False, None]).any()
evaluates to None
.)
Appears to happen in Python 3.6.1, Numpy 1.12.1, on Linux, as well as Python 2.7.10, numpy 1.9.2. on Windows (10).
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Series.any() and .all() don't return bool values if dtype=object
ok, I think we will add a bool() if its a scalar on the pandas side for compat. @jdavies1618 want to do a...
Read more >How exactly does the behavior of Python bool and numpy ...
In the first case the array dtype is boolean. The elements represent boolean values, but they are not, themselves, Python True/False objects ......
Read more >9. Numpy: Boolean Indexing | Numerical Programming
A two-dimensional array is returned. Every row corresponds to a non-zero element. np.transpose(a.
Read more >pandas.Series.any — pandas 1.5.2 documentation
Return whether any element is True, potentially over an axis. ... Not implemented for Series. skipnabool ... df.any() A True B True C...
Read more >Data type objects (dtype) — NumPy v1.24 Manual
The 24 built-in array scalar type objects all convert to an associated data-type object. This is true for their sub-classes as well. Note...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think we should just document the current behavior.
.all()
is essentially defined aslogical_or.reduce()
, andlogical_or
’sobject
implementation chose by design to replicate Python’sor
shortcut semantics.Editing the documentation is fine as well, of course. But I’d still find it a little bit funky that
array([5,2]).any()
would evaluate to something other thanarray([5,2], dtype=object).any()
.