Ndindex

1.10.1 · active · verified Fri Apr 10

ndindex is a Python library designed for representing and manipulating objects that can serve as valid indices for NumPy arrays, including slices, integers, ellipses, None, and integer/boolean arrays, and tuples containing these types. It provides a uniform API for these objects, ensuring correct semantics aligned with NumPy's `ndarray` indexing rules. The current version is 1.10.1, and it maintains an active release cadence with recent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating `ndindex` objects from basic Python indices, canonicalizing slices (both generally and for a specific array shape), and converting an `ndindex` object back into a raw Python index suitable for use with NumPy arrays. It highlights the `reduce()` method for canonicalization and `raw` attribute for NumPy compatibility.

import numpy as np
from ndindex import ndindex, Slice, Tuple

# Create an ndindex object from a Python slice
idx_slice = ndindex(slice(1, 10, 2))
print(f"Ndindex from slice: {idx_slice}")

# Canonicalize a slice (reduce to simplest form)
canonical_slice = Slice(None, 10).reduce()
print(f"Canonical slice: {canonical_slice}")

# Canonicalize for a specific array shape
shaped_slice = Slice(-5, 10, 2).reduce(12)
print(f"Slice reduced for shape 12: {shaped_slice}")

# Manipulate a tuple index
tuple_idx = Tuple(0, slice(0, 5), None, 1)
print(f"Tuple index: {tuple_idx}")

# Get the raw Python index to use with NumPy
np_array = np.arange(20).reshape(2, 10)
raw_index = tuple_idx.raw
print(f"Raw Python index: {raw_index}")

# Use the raw index with a NumPy array
try:
    indexed_array = np.arange(100).reshape(10, 10)[raw_index] # Example with a 2D array
    print(f"Indexed array shape: {indexed_array.shape}")
except IndexError as e:
    print(f"Indexing with {raw_index} failed due to: {e}")

view raw JSON →