Skip to main content

XArray-Histogram

Compute and manipulate histograms from XArray data using BoostHistogram

PyPI GitHub release test status codecov Documentation Status

This package allows to compute histograms from and to XArray data. It relies on the Boost Histogram library giving better performances compared to numpy.histogram and the existing xhistogram. It also brings features such as integer/discrete bins or periodic bins.

Dask arrays are supported.

Vectorized manipulation and analysis of the resulting histogram(s) is provided via an XArray accessor.

Quick examples

Three functions are provided (histogram, histogram2d, and historamdd), similar to those from Numpy:

import xarray_histogram as xh
hist = xh.histogram(data, bins=100, range=(0, 10))

Bins can be specified directly via Boost axes for a finer control. The equivalent of the example above would be:

import boost_histogram.axis as bha
hist = xh.histogram(data, bins=[bha.Regular(100, 0., 10.)])

Multi-dimensional histogram can be computed, here in 2D for instance:

hist = xh.histogramdd(
    temp, chlorophyll,
    bins=[bha.Regular(100, -5., 40.), bha.Regular(100, 1e-3, 10, transform=bha.transform.log))
)

The histograms can be computed on the whole flattened arrays, but we can apply it to only some dimensions. For instance if we have an array of dimensions (time, lat, lon) we can retrieve the time evolution of its histogram:

hist = xh.histogram(temp, bins=[bha.Regular(100, 0., 10.)], dims=['lat', 'lon'])

Weights can be applied. Output histogram can be normalized

Accessor

An Xarray accessor is provided to do some vectorized manipulations on histogram data. Simply import xarray_histogram.accessor, and all arrays can then access methods through the hist property::

import xarray_histogram.accessor

hist = xh.histogram(temp, ...)

hist.hist.edges()
hist.hist.median()
hist.hist.ppf(q=0.75)

See the documentation for more details.

Documentation

Documentation available at https://xarray-histogram.readthedocs.io

Installation

From PyPI:

pip install xarray-histogram

From source:

git clone https://github.com/Descanonge/xarray-histogram
cd xarray-histogram
pip install -e .

TODO

Some features of Boost are not yet available:

  • Growing axes: Dask requires to know in advance the size of output chunks. This could reasonably be supported, at least when applying over the whole array (no looping dimensions).
  • Advanced storage/accumulators: they provide additional values on top of the count of samples falling into a bin. They require more than one number per bin, and a more complex sum of two histograms (possibly making histogram along chunked dimensions impossible).
  • The Unified Histogram Indexing could be implemented in the accessor to facilitate manipulation of histogram arrays.

Requirements

Tests and performance

To compare performances check this notebook.

Other packages

xhistogram already exists. It relies on Numpy functions (searchsorted) and thus does not benefit of some performance upgrades brought by Boost (see performance comparisons).

dask-histogram ports Boost-histogram for Dask. It does not support multi-dimensional arrays: one can still reshape the input array but this can incur performance penalties. Still, as it works directly with boost objects rather than Dask arrays all features of Boost should be available.

Metadata

Release files for xarray-histogram 0.2.2

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

Source distribution (sdist)

Source distribution for xarray-histogram 0.2.2
File Size Uploaded
xarray_histogram-0.2.2.tar.gz 21.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xarray-histogram 0.2.2
File Interpreter ABI Platform
xarray_histogram-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / xarray_histogram-0.2.2.tar.gz

Download URL xarray_histogram-0.2.2.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8f60b19e7dee2c6c4e55cdd5fcb42f1ef2b34b78e94c25b6997f0092ce1a911b
BLAKE2b-256 checksum
How to use checksums
f05ec97ffb58df05343cc1ea34bea5658f051b0100e555183b17e0de8d05b909
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 27, 2025.

Transparency log

Release files / xarray_histogram-0.2.2-py3-none-any.whl

Download URL xarray_histogram-0.2.2-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90682f9575131e5ea5d96feba5f8679e8c3fae039018d5e38f497696d55ccd5c
BLAKE2b-256 checksum
How to use checksums
1438d1a8b0c8b7749fde76daa12ec3e63aa052cf37cacc2e9715377ce0197a99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 27, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

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