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 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
31 changes: 31 additions & 0 deletions doc/release/upcoming_changes/28590.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Fix ``flatiter`` indexing edge cases
------------------------------------

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

- Boolean non-array indices that do not match the axis's size, like
``arr.flat[[True, True]]`` were incorrectly treated as
``arr.flat[np.array([1, 1])]``. They now raise an index error.
(This is expected to not raise in the future).
- Float non-array indices were also cast to integer and incorrectly
treated as ``arr.flat[np.array([1.0, 1.0])]``. This is now deprecated
and will be removed in a future version.
- 0-dimensional boolean indices like ``arr.flat[True]`` are also
deprecated and will be removed in a future version.

* 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