Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#17175·numpy

BUG: Boolean indexing broken in `np.flatiter`

Author: BvB93Created Aug 28, 2020Updated Sep 30, 2026
Labels00 - Bugcomponent: numpy._coresustain-2026

NOTE(seberg): This is correctly deprecated now, but we can finalize the deprecation already.

While attempting to address https://github.com/numpy/numpy/issues/17113 I stumbled upon an issue with flatiter and boolean indexing: It appears that the latter only works as intended if a boolean array is passed. If, for example, a list of booleans is passed instead then they're treated as normal integers. As can be seen below, things get even more "creative" when dealing with plain booleans.

Note that the flatiter documentation does claim that advanced indexing is supported (docs).

Reproducing code example:

python
In [1]: import sys; import numpy as np

In [2]: sys.version
Out[2]: '3.8.3 | packaged by conda-forge | (default, Jun  1 2020, 17:21:09) \n[Clang 9.0.1 ]'

In [3]: np.__version__
Out[3]: '1.20.0.dev0+32b3f82'

In [4]: a: np.flatiter = np.arange(4).flat

# Behaves as expected
In [5]: a[np.array([True, True, True, True])]
Out[5]: array([0, 1, 2, 3])

# The booleans are treated as normal integers
In [6]: a[[True, True, True, True]]
Out[6]: array([1, 1, 1, 1])

# Not even sure what's going on at this point
In [7]: a[True]                                                                                            
Out[7]: 0

In [8]: a[False]                                                                                           
Out[8]: array([], dtype=int64)

In [9]: a[np.bool_(True)]                                                                                  
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-b696e95f8475> in <module>
----> 1 a[np.bool_(True)]

IndexError: unsupported iterator index

Source: numpy/numpy

View original on GitHubView discussion on GitHub