Skip to main content

labelarray

Labelled N-D arrays in pure NumPy, with exact label semantics and reproducible reductions. It needs NumPy only; xarray is used only to convert to and from xarray objects. It is the array layer of tidy3a, released on its own.

It is deliberately small. What it offers that other array libraries do not:

  1. Exact labels. Labels match only when they are the same kind (booleans, numbers, times, durations, text, bytes) and exactly equal, in lookups, alignment, and joins. The integer 2**53 + 1 never matches the float 2**53; pandas, and so xarray, pair labels through float64.
  2. Reproducible reductions. Float32 sums and means are correctly rounded: the float32 nearest the exact result, the same in any memory layout. Grouped reductions are bit-identical to reducing each group on its own.
  3. Collections. Arrays sharing axes, with one label array per axis, joined and aligned under those rules.

Status: version 0.1.1; the API may still change before 1.0. What changed in each release is in CHANGELOG.md.

Installing

Requires Python 3.11 or newer.

pip install labelarray                  # NumPy only
pip install "labelarray[xarray]"        # with xarray: to_xarray() and from_xarray()
pip install "labelarray[fast]"          # with Numba: compiled grouped float32 sums and means

Results are the same with or without fast; it only makes large grouped sums and means faster.

Arrays

An Array is a NumPy array with one name per axis, labels for any of its axes, attributes, and a name:

import numpy as np
from labelarray import Array

air = Array(
    np.array([[271.0, 272.0, 273.0], [281.0, 282.0, 283.0]], dtype="float32"),
    dims=("lat", "time"),
    coords={"lat": [10.0, 20.0], "time": np.arange("2026-01-01", "2026-01-04", dtype="datetime64[D]")},
    attrs={"units": "K"},
    name="air",
)
print(air.sel(lat=20.0).data)  # [281. 282. 283.]
print(air.isel(time=[0, 2]).labels("time"))  # ['2026-01-01' '2026-01-03']
print(air.reduce("mean", ["time"]).data)  # [272. 282.]

isel takes positions, slices, or masks; sel takes labels, looked up exactly; one label or position removes the axis, a list keeps it. transpose, rename (axes), reduce (sum, mean, min, max, median, std, var, any, all, count, with na_rm and min_count), and dot (the sum of products over named axes) complete the methods.

NumPy's ufuncs, np.where, and Python's operators broadcast by axis name. Labels on a shared axis must match: the same labels in another order are matched by label, and anything else is an AlignError. There is no outer join by default; ask for one with Collection.join.

import numpy as np
from labelarray import Array

a = Array([1.0, 2.0, 3.0], ("x",), {"x": [1, 2, 3]})
b = Array([30.0, 10.0, 20.0], ("x",), {"x": [3, 1, 2]})
print((a + b).data)  # [11. 22. 33.]
print(np.sqrt(a * 4).data)  # [2.         2.82842712 3.46410162]

Labels are copied once into a read-only array the library owns, and each label array's index is built on first use and kept with it: a sorted index, a sort-once index for unsorted labels, or a grid index for evenly spaced numbers and times, which looks labels up arithmetically. register_index adds index classes for other label types, under the same exactness obligations.

Collections

A Collection holds variables with different axes, each axis with one size and at most one label array:

from labelarray import Array, Collection

left = Collection({"a": Array([1.0, 2.0, 3.0], ("k",))}, {"k": [1, 3, 5]})
right = Collection({"b": Array([10.0, 20.0], ("k",))}, {"k": [5, 1]})
both = left.join(right, "inner")
print(both.coords["k"])  # [1 5]
print(both.array("b").data)  # [20. 10.]
print(left.join(right, "left").array("b").data)  # [20. nan 10.]

isel, sel, select (variables), assign, rename (variables), join (left, inner, or outer), nearest (each label matched to its nearest within a tolerance), to_dict, to_xarray, and from_xarray are its methods. Missing positions after a join promote integers and booleans as xarray does, and attributes that conflict are dropped.

Grouping

A Grouping assigns each position a group code; factorize makes one from key values, optionally after a key transform (Bins, Duration, Round, GridCells), with every missing key (NaN, NaT) one group, dropped unless asked for. grouped.reduce reduces every group at once:

import numpy as np
from labelarray import Array, Grouping, factorize, grouped

intensity = Array(np.array([10, 20, 30, 40, 50], dtype="float32"), ("point",))
codes, labels = factorize(np.array([0, 0, 1, 1, 2]))
rings = Grouping(codes, labels)
print(grouped.reduce(intensity, "mean", rings, "point", "ring").data)  # [15. 35. 50.]
print(grouped.counts(rings, "ring").data)  # [2 2 1]

grouped.broadcast puts a result per group back on each position.

Errors

Every error is a LabelArrayError (a ValueError): LabelError for labels that cannot be found, ordered, or compared; AlignError for axes that cannot be paired; GroupError for keys that cannot be grouped; and ShapeError for axis names, sizes, and ranks that do not fit.

Development

From a checkout, install the development dependencies and run the tests:

python -m pip install -e ".[dev]"
python -m pytest -q

xarray and pandas are test dependencies: the tests check the library against xarray and its label lookups against pandas. Before pushing, run the CI checks in fresh environments with only declared dependencies (requires uv):

scripts/check.sh

The release procedure describes the GitHub Release workflow.

License

MIT

Metadata

Release files for labelarray 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for labelarray 0.1.1
File Size Uploaded Provenance Actions
labelarray-0.1.1.tar.gz 73.7 kB With provenance Details for labelarray-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for labelarray 0.1.1
File Interpreter ABI Platform Actions
labelarray-0.1.1-py3-none-any.whl Python 3 none any Details for labelarray-0.1.1-py3-none-any.whl

Total release size: 124.5 kB

Release files / labelarray-0.1.1.tar.gz

Download URL labelarray-0.1.1.tar.gz
Size 73.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5abe73054fa8ae28c798c006c65f7f4ef55a4c42fec3e2adfaf3777deef4973b
BLAKE2b-256 checksum
How to use checksums
5ce09f7a153b66bba8fc6601878942be8fb8f2d47814e7eea198fac025385baf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / labelarray-0.1.1-py3-none-any.whl

Download URL labelarray-0.1.1-py3-none-any.whl
Size 50.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d760bad4c646372aab77072681c5ff01f970dc6ac6fc9399e259461ce27ca75
BLAKE2b-256 checksum
How to use checksums
bde25ed5d75eb4254c25d0470ade04c59e0fded30cbbbce43c3dde58bde0049e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for labelarray 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for labelarray-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page