ndcube
raw JSON → 2.4.0 verified Mon Apr 27 auth: no python
A Python package for multi-dimensional contiguous and non-contiguous coordinate aware arrays, built on sunpy and astropy. Current version 2.4.0, release cadence irregular. Works with NumPy arrays and supports powerful slicing and dicing with World Coordinate System (WCS) awareness.
pip install ndcube Common errors
error AttributeError: 'NDCube' object has no attribute 'unit' ↓
cause ndcube v2.0 removed the 'unit' attribute in favor of astropy unit in the WCS.
fix
Access units via cube.wcs.world_axis_units or similar astropy properties.
error ImportError: cannot import name 'NDCubeSequence' from 'ndcube' ↓
cause NDCubeSequence was removed in ndcube v2.1.
fix
Use ndcube.NDCollection instead of ndcube.NDCubeSequence.
error ValueError: The dimension of the WCS does not match the data. ↓
cause Data array has fewer dimensions than WCS expects (common when slicing).
fix
Ensure the data shape matches the number of WCS axes, or use cube.drop_axis() after slicing.
Warnings
breaking API change in v2.0: NDCube.crop returns a new NDCube, not a tuple of slices. ↓
fix If upgrading from v1.x, update code that assigns crop result to multiple variables.
deprecated NDCubeSequence is deprecated since v2.1; use NDCollection instead. ↓
fix Replace ndcube.NDCubeSequence with ndcube.NDCollection.
gotcha When slicing an NDCube, the result may drop WCS information if the slice is not a view (e.g., fancy indexing). Always check cube.wcs after slicing. ↓
fix Use boolean or integer array indexing with caution; prefer basic slicing.
gotcha Installing from source may require Cython and numpy headers if not using wheels. Wheels are provided for common platforms, but on rare architectures you may need to build. ↓
fix Install via conda (conda install -c conda-forge ndcube) to avoid compilation.
Install
pip install ndcube[all] Imports
- NDCube wrong
from ndcube.ndcube import NDCubecorrectfrom ndcube import NDCube - NDCollection wrong
from ndcube.collection import NDCollectioncorrectfrom ndcube import NDCollection
Quickstart
import numpy as np
from ndcube import NDCube, NDCollection
# Create a simple 2D array
data = np.random.rand(10, 10)
wcs = None # For simplicity; in practice, provide astropy WCS
cube = NDCube(data, wcs)
print(cube)
print(cube.shape)
# Slice along first axis
slice1 = cube[0]
print(slice1.shape)
# Create collection of cubes
cubes = [NDCube(np.random.rand(5,5), None) for _ in range(3)]
collection = NDCollection(cubes, meta={'source': 'test'})
print(collection)