Skip to main content

A Python library for manipulating indices of ndarrays.

Project description

ndindex

ndindex logo

A Python library for manipulating indices of ndarrays.

The documentation for ndindex can be found at https://quansight-labs.github.io/ndindex/

ndindex is a library that allows representing and manipulating objects that can be valid indices to numpy arrays, i.e., slices, integers, ellipses, None, integer and boolean arrays, and tuples thereof. The goals of the library are

  • Provide a uniform API to manipulate these objects. Unlike the standard index objects themselves like slice, int, and tuple, which do not share any methods in common related to being indices, ndindex classes can all be manipulated uniformly. For example, idx.args always gives the arguments used to construct idx.

  • Give 100% correct semantics as defined by numpy's ndarray. This means that ndindex will not make a transformation on an index object unless it is correct for all possible input array shapes. The only exception to this rule is that ndindex assumes that any given index will not raise IndexError (for instance, from an out of bounds integer index or from too few dimensions). For those operations where the array shape is known, there is a reduce() method to reduce an index to a simpler index that is equivalent for the given shape.

  • Enable useful transformation and manipulation functions on index objects.

Examples

Canonicalize a slice (over a given shape, or independent of array shape)

>>> from ndindex import *
>>> Slice(-2, 10, 3).reduce()
Slice(-2, 10, 2)
>>> Slice(-2, 10, 3).reduce(5)
Slice(3, 4, 1)

Compute the maximum length of a sliced axis

>>> import numpy as np
>>> len(Slice(2, 10, 3))
3
>>> len(np.arange(10)[2:10:3])
3

Compute the shape of an array of shape (10, 20) indexed by [0, 0:10]

>>> Tuple(0, slice(0, 10)).newshape((10, 20))
(10,)
>>> np.ones((10, 20))[0, 0:10].shape
(10,)

Check if an indexed array would be empty

>>> Tuple(0, ..., Slice(10, 20)).isempty((3, 4, 5))
True
>>> np.ones((3, 4, 5))[0,...,10:20]
array([], shape=(4, 0), dtype=float64)

See the documentation for full details on what ndindex can do.

License

MIT License

Acknowledgments

ndindex development is supported by Quansight Labs and is sponsored in part by the D. E. Shaw group. The D. E. Shaw group collaborates with Quansight on numerous open source projects, including Numba, Dask and Project Jupyter.

https://labs.quansight.org/ https://www.deshaw.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ndindex-1.6.tar.gz (77.4 kB view details)

Uploaded Source

Built Distribution

ndindex-1.6-py3-none-any.whl (77.1 kB view details)

Uploaded Python 3

File details

Details for the file ndindex-1.6.tar.gz.

File metadata

  • Download URL: ndindex-1.6.tar.gz
  • Upload date:
  • Size: 77.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/58.5.3 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for ndindex-1.6.tar.gz
Algorithm Hash digest
SHA256 752e5a05c39883824316fdd7f7b650f6860eb71cecb9f15d811be2d879bebf02
MD5 2408bd59ec82fe1ed319fdfeb37a0da6
BLAKE2b-256 de97ec37f59dae9a762623851859af5e434cac25473c6c09ef60d35020e4a2ef

See more details on using hashes here.

Provenance

File details

Details for the file ndindex-1.6-py3-none-any.whl.

File metadata

  • Download URL: ndindex-1.6-py3-none-any.whl
  • Upload date:
  • Size: 77.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 importlib_metadata/4.10.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for ndindex-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5c1158b6dcaf4e2b79c329d27985361bfeadaa4b3dc9e929a370abefddf9d4f0
MD5 2cc5fb33db0ba542861e890cf4aebc1f
BLAKE2b-256 a3987ca8e9d837aba1b2c68ead618b6568c1dadc3616d3a841a45db41b755c6f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page