tsam_xarray
DataArray in, DataArray out — multi-dimensional time series aggregation with tsam and xarray.
The problem
Energy system data is multi-dimensional — variables, regions, scenarios, years. Some dimensions should be clustered together (solar and wind profiles in the same region should see the same typical days), while others need independent clustering (each scenario has its own weather patterns).
tsam works on flat DataFrames. With multi-dimensional data, you end up writing boilerplate: loop over scenarios, convert to DataFrame, aggregate, extract results, convert back, concatenate, hope the dims line up. Accuracy metrics come back as unlabeled pd.Series. Saving a clustering means managing raw dicts.
The solution
import tsam_xarray
result = tsam_xarray.aggregate(
da, # (time, variable, region, scenario)
time_dim="time",
cluster_dim=["variable", "region"], # clustered together
n_clusters=4,
)
# scenario is sliced independently — each gets its own clustering
Everything comes back as labeled xarray objects:
result.cluster_representatives # (scenario, cluster, timestep, variable, region)
result.reconstructed # same shape as input
result.cluster_assignments # (scenario, period)
Accuracy metrics preserve all dimensions — see exactly where the approximation is good or bad:
result.accuracy.rmse # DataArray (scenario, variable, region)
result.accuracy.weighted_rmse # DataArray (scenario,) — per-slice summary
Save, load, reuse
# Save clustering (not the data — just the mapping)
result.clustering.to_json("clustering.json")
# Load and inspect — no original data needed
clustering = tsam_xarray.load_clustering("clustering.json")
clustering.n_clusters # 4
clustering.cluster_assignments # DataArray (scenario, period)
clustering.cluster_occurrences # DataArray (scenario, cluster)
# Apply to new data or disaggregate optimization results
new_result = clustering.apply(new_da)
full_timeseries = clustering.disaggregate(optimized_data)
Tuning
Find optimal hyperparameters across all slices:
grid = tsam_xarray.grid_search(
da,
time_dim="time",
cluster_dim=["variable", "region"],
timesteps=np.geomspace(2, 48, num=12, dtype=int), # sparse search
)
grid.summary_matrix["rmse"] # heatmap-ready (n_clusters, n_segments)
grid.accuracy["weighted_rmse"] # per-slice weighted RMSE for every config
Installation
pip install tsam-xarray
Documentation
Full docs with interactive examples: tsam-xarray.readthedocs.io
Metadata
Release files for tsam-xarray 0.6.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tsam_xarray-0.6.7.tar.gz | 582.1 kB | With provenance | Details for tsam_xarray-0.6.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tsam_xarray-0.6.7-py3-none-any.whl | Python 3 | none | any | Details for tsam_xarray-0.6.7-py3-none-any.whl |
Total release size: 614.9 kB
Release files / tsam_xarray-0.6.7.tar.gz
| Download URL | tsam_xarray-0.6.7.tar.gz |
|---|---|
| Size | 582.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c9b8481ad5ff41031cd545163f1366503c0f52b527403d0a2252f0e18ae4533
|
|
BLAKE2b-256 checksum How to use checksums |
e933b8e08ff460e3152353bc98e9ff9b0627f53d05f7014fa46ba3ebee14ddbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 8, 2026.
Transparency logRelease files / tsam_xarray-0.6.7-py3-none-any.whl
| Download URL | tsam_xarray-0.6.7-py3-none-any.whl |
|---|---|
| Size | 32.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4d8536bf28ae27327c2f723d0f777195a429ed32f7409dd5ba07e6855e97c3f9
|
|
BLAKE2b-256 checksum How to use checksums |
17b755709e41d97629e65fc6b9480f88aa5fedcac083e67ca94c704ed5fa1c16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 8, 2026.
Transparency logSecurity for tsam-xarray 0.6.7
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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for tsam_xarray-0.6.7.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 Sep 8, 2026.
Transparency logA 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.