An implementation of chunked, compressed, N-dimensional arrays for Python
The fundamental array and group classes that form the core of zarr's object-oriented interface. These classes provide comprehensive functionality for array manipulation and hierarchical data organization.
class Array:
"""Synchronous zarr array supporting chunked, compressed N-dimensional arrays."""
# Core Properties
shape: tuple[int, ...]
dtype: np.dtype
chunks: tuple[int, ...]
size: int
nbytes: int
nchunks: int
cdata_shape: tuple[int, ...]
# Metadata
attrs: Attributes
metadata: ArrayMetadata
name: str
path: str
basename: str
# Storage
store: Store
store_path: StorePath
def __init__(
self,
store_path: StorePath,
metadata: ArrayMetadata,
order: MemoryOrder = 'C'
): ...def __getitem__(self, selection: Selection) -> np.ndarray: ...
def __setitem__(self, selection: Selection, value: Any) -> None: ...
def get_basic_selection(
self,
selection: BasicSelection,
out: np.ndarray = None,
fields: Fields = None
) -> np.ndarray: ...
def set_basic_selection(
self,
selection: BasicSelection,
value: Any,
fields: Fields = None
) -> None: ...
def get_orthogonal_selection(
self,
selection: OrthogonalSelection,
out: np.ndarray = None,
fields: Fields = None
) -> np.ndarray: ...
def set_orthogonal_selection(
self,
selection: OrthogonalSelection,
value: Any,
fields: Fields = None
) -> None: ...
def get_coordinate_selection(
self,
selection: CoordinateSelection,
out: np.ndarray = None,
fields: Fields = None
) -> np.ndarray: ...
def set_coordinate_selection(
self,
selection: CoordinateSelection,
value: Any,
fields: Fields = None
) -> None: ...
def get_mask_selection(
self,
selection: MaskSelection,
out: np.ndarray = None,
fields: Fields = None
) -> np.ndarray: ...
def set_mask_selection(
self,
selection: MaskSelection,
value: Any,
fields: Fields = None
) -> None: ...def resize(self, *args: int) -> None: ...
def append(
self,
data: ArrayLike,
axis: int = 0
) -> None: ...
def create_chunk(self, chunk_coords: tuple[int, ...]) -> None: ...@property
def oindex(self) -> OIndex: ...
@property
def vindex(self) -> VIndex: ...
@property
def blocks(self) -> BlockIndex: ...def info(self) -> ArrayInfo: ...
def __array__(self, dtype: np.dtype = None) -> np.ndarray: ...
def __repr__(self) -> str: ...
def __str__(self) -> str: ...class AsyncArray(Generic[ArrayMetadata]):
"""Asynchronous zarr array with same functionality as Array but async methods."""
# Same properties as Array
shape: tuple[int, ...]
dtype: np.dtype
chunks: tuple[int, ...]
size: int
nbytes: int
nchunks: int
attrs: Attributes
metadata: ArrayMetadata
def __init__(
self,
store_path: StorePath,
metadata: ArrayMetadata,
order: MemoryOrder = 'C'
): ...
# Async data access methods
async def getitem(
self,
selection: Selection,
prototype: BufferPrototype = None
) -> NDBuffer: ...
async def setitem(
self,
selection: Selection,
value: NDArrayLike
) -> None: ...
async def resize(self, *args: int) -> None: ...
async def info(self) -> ArrayInfo: ...class Group:
"""Synchronous zarr group for hierarchical organization of arrays and subgroups."""
# Core Properties
attrs: Attributes
metadata: GroupMetadata
name: str
path: str
basename: str
# Storage
store: Store
store_path: StorePath
def __init__(
self,
store_path: StorePath,
metadata: GroupMetadata = None
): ...def __getitem__(self, key: str) -> Union[Array, Group]: ...
def __setitem__(self, key: str, value: Any) -> None: ...
def __delitem__(self, key: str) -> None: ...
def __contains__(self, key: str) -> bool: ...
def __iter__(self) -> Iterator[str]: ...
def __len__(self) -> int: ...
def keys(self) -> Iterator[str]: ...
def values(self) -> Iterator[Union[Array, Group]]: ...
def items(self) -> Iterator[tuple[str, Union[Array, Group]]]: ...
@property
def members(self) -> dict[str, Union[Array, Group]]: ...def create_array(
self,
name: str,
shape: ShapeLike,
dtype: DTypeLike = 'float64',
chunks: ChunksLike = None,
compressor: CompressorLike = 'default',
fill_value: Any = None,
order: str = 'C',
filters: FiltersLike = None,
cache_metadata: bool = True,
cache_attrs: bool = True,
read_only: bool = False,
object_codec: Any = None,
dimension_separator: str = None,
write_empty_chunks: bool = True,
overwrite: bool = False,
**kwargs
) -> Array: ...
def array(
self,
name: str,
data: ArrayLike,
**kwargs
) -> Array: ...
def empty(
self,
name: str,
shape: ShapeLike,
**kwargs
) -> Array: ...
def zeros(
self,
name: str,
shape: ShapeLike,
**kwargs
) -> Array: ...
def ones(
self,
name: str,
shape: ShapeLike,
**kwargs
) -> Array: ...
def full(
self,
name: str,
shape: ShapeLike,
fill_value: Any,
**kwargs
) -> Array: ...def create_group(
self,
name: str,
overwrite: bool = False,
cache_attrs: bool = True,
**kwargs
) -> Group: ...
def require_group(
self,
name: str,
overwrite: bool = False
) -> Group: ...def info(self) -> GroupInfo: ...
def tree(self, expand: bool = False, level: int = None) -> Any: ...
def __repr__(self) -> str: ...class AsyncGroup:
"""Asynchronous zarr group with same functionality as Group but async methods."""
# Same properties as Group
attrs: Attributes
metadata: GroupMetadata
name: str
path: str
# Async methods mirror Group methods
async def getitem(self, key: str) -> Union[AsyncArray, AsyncGroup]: ...
async def setitem(self, key: str, value: Any) -> None: ...
async def delitem(self, key: str) -> None: ...
async def contains(self, key: str) -> bool: ...
async def create_array(self, name: str, **kwargs) -> AsyncArray: ...
async def create_group(self, name: str, **kwargs) -> AsyncGroup: ...# Selection types
Selection = Union[BasicSelection, OrthogonalSelection, CoordinateSelection, MaskSelection]
BasicSelection = Union[slice, int, tuple]
OrthogonalSelection = tuple[Union[slice, int, np.ndarray], ...]
CoordinateSelection = tuple[np.ndarray, ...]
MaskSelection = np.ndarray
# Data types
ArrayLike = Union[np.ndarray, Array, list, tuple]
ShapeLike = Union[int, tuple[int, ...]]
DTypeLike = Union[str, np.dtype, type]
Fields = Union[str, list[str]]
# Memory order
MemoryOrder = Literal['C', 'F']import zarr
import numpy as np
# Create and access array
z = zarr.zeros((1000, 1000), chunks=(100, 100))
# Basic indexing
z[0, :] = 1.0
data = z[:10, :10]
# Advanced indexing
z.oindex[0, :] = 2.0 # Orthogonal indexing
z.vindex[[0, 1], [0, 1]] = 3.0 # Vectorized indexing
# Block access
for block in z.blocks:
print(block.shape)
# Array modification
z.resize(2000, 2000)
z.append(np.ones((100, 1000)), axis=0)# Create group and add arrays
grp = zarr.group()
grp.create_array('temperature', shape=(365, 100, 100), chunks=(1, 50, 50))
grp.create_array('humidity', shape=(365, 100, 100), chunks=(1, 50, 50))
# Create nested groups
sub_grp = grp.create_group('processed_data')
sub_grp.create_array('average_temp', shape=(365,), chunks=(100,))
# Access data
temp_data = grp['temperature']
avg_temp = grp['processed_data/average_temp']
# Iterate through group
for name, item in grp.items():
if isinstance(item, zarr.Array):
print(f"Array {name}: shape={item.shape}")
elif isinstance(item, zarr.Group):
print(f"Group {name}: {len(item)} members")# Set array attributes
z = zarr.zeros((100, 100))
z.attrs['units'] = 'temperature'
z.attrs['scale_factor'] = 0.1
z.attrs['add_offset'] = 273.15
# Set group attributes
grp = zarr.group()
grp.attrs['title'] = 'Weather Dataset'
grp.attrs['created'] = '2024-01-01'
# Access attributes
print(z.attrs['units'])
print(grp.attrs['title'])Install with Tessl CLI
npx tessl i tessl/pypi-zarr