question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

any() for object-dtype arrays returns non-booleans

See original GitHub issue

The 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:open
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rkerncommented, Jan 29, 2018

I think we should just document the current behavior. .all() is essentially defined as logical_or.reduce(), and logical_or’s object implementation chose by design to replicate Python’s or shortcut semantics.

0reactions
acdr000commented, Jan 30, 2018

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 than array([5,2], dtype=object).any().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found