Skip to content

ENH: Use array indexing preparation routines for flatiter objects #28590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
19 changes: 19 additions & 0 deletions benchmarks/benchmarks/bench_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def setup(self):
self.m_half = np.copy(self.m_all)
self.m_half[::2] = False
self.m_none = np.repeat(False, 200 * 50000)
self.m_index_2d = np.arange(200 * 50000).reshape((100, 100000))

def time_flat_bool_index_none(self):
self.a.flat[self.m_none]
Expand All @@ -143,3 +144,21 @@ def time_flat_bool_index_half(self):

def time_flat_bool_index_all(self):
self.a.flat[self.m_all]

def time_flat_fancy_index_2d(self):
self.a.flat[self.m_index_2d]

def time_flat_empty_tuple_index(self):
self.a.flat[()]

def time_flat_ellipsis_index(self):
self.a.flat[...]

def time_flat_bool_index_0d(self):
self.a.flat[True]

def time_flat_int_index(self):
self.a.flat[1_000_000]

def time_flat_slice_index(self):
self.a.flat[1_000_000:2_000_000]
30 changes: 30 additions & 0 deletions doc/release/upcoming_changes/28590.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Improve `flatiter` indexing by using ndarray index preparation
--------------------------------------------------------------

The `flatiter`` object now shares the same index preparation logic as
`ndarray`, ensuring consistent behavior and fixing several issues where
invalid indices were previously accepted or misinterpreted.

Key fixes and improvements:

* Stricter index validation

``arr.flat[[True, True]]`` and ``arr.flat[[1.0, 1.0]]`` were incorrectly
treated as ``arr.flat[[1, 1]]``. They now raise an `IndexError`` (unless
``arr.flat[[True, Truee]]`` is a valid boolean index)

* Fixed crash when assigning to an empty index tuple:

``arr.flat[()] = 0`` previously crashed the Python interpreter. It now
correctly assigns to the entire array.

* Consistent error types:

Certain invalid `flatiter` indices that previously raised `ValueError`
now correctly raise `IndexError`, aligning with `ndarray` behavior.

* Improved error messages:

The error message for unsupported index operations now provides more
specific details, including explicitly listing the valid index types,
instead of the generic ``IndexError: unsupported index operation``.
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy