Indexable#

class named_arrays.Indexable#

Bases: ABC

A mixin for immutable composite objects whose fields are named arrays.

This class provides a shape and a named __getitem__ by delegating to named_arrays.shape() and named_arrays.getitem(), both of which recurse into the dataclasses fields of the object. A subclass is therefore expected to be an (immutable) dataclasses instance whose fields are named arrays, or nested structures thereof.

The ndim, size, and axes properties are derived from shape, so a subclass usually needs to define only its fields.

Warning

Do not combine this mixin with named_arrays.AbstractArray or any other object recognized by named_arrays.named_array_like(). For such objects named_arrays.shape() and named_arrays.getitem() index the object directly, which would recurse back into the properties defined here and never terminate.

Attributes

axes

The names of the axes of this object, tuple(self.shape).

ndim

The number of dimensions of this object, len(self.shape).

shape

The broadcasted shape of every array-like field of this object.

size

The total number of elements in this object, the product of the values of shape.

Methods

__init__()

isel(**item)

Index every array-like field of this object along named axes given as keyword arguments.

Inheritance Diagram

Inheritance diagram of named_arrays.Indexable
isel(**item)#

Index every array-like field of this object along named axes given as keyword arguments.

This is a convenience wrapper around __getitem__(): a.isel(x=0) is equivalent to a[dict(x=0)].

Since keyword-argument names must be valid Python identifiers, axes whose names are not valid identifiers can only be indexed using the dict form, a[{...}].

Parameters:
Return type:

Self

property axes: tuple[str, ...]#

The names of the axes of this object, tuple(self.shape).

property ndim: int#

The number of dimensions of this object, len(self.shape).

property shape: dict[str, int]#

The broadcasted shape of every array-like field of this object.

property size: int#

The total number of elements in this object, the product of the values of shape.