Fix AlignmentError in to_xarray_dataset when data variables have different setpoints - #8244
Open
jenshnielsen with Copilot wants to merge 3 commits into
Open
Fix AlignmentError in to_xarray_dataset when data variables have different setpoints#8244jenshnielsen with Copilot wants to merge 3 commits into
jenshnielsen with Copilot wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8244 +/- ##
==========================================
+ Coverage 71.09% 71.11% +0.01%
==========================================
Files 305 305
Lines 31934 31947 +13
==========================================
+ Hits 22705 22718 +13
Misses 9229 9229 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Copilot
AI
changed the title
[WIP] Fix export to xarray dataset when data variables have different setpoints
Fix AlignmentError in to_xarray_dataset when data variables have different setpoints
Jun 24, 2026
Collaborator
|
@copilot Please look into the comment on this pr |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an AlignmentError in DataSet.to_xarray_dataset() caused by merging per-parameter sub-datasets that represent a shared setpoint coordinate differently (standalone index vs MultiIndex component), by normalizing conflicting MultiIndex sub-datasets before xr.merge.
Changes:
- Detect merge conflicts between MultiIndex level coordinates and standalone dimensions and resolve them by
unstack("multi_index")prior toxr.merge. - Add a regression test covering mixed setpoint structures (non-grid array param + scalar param sharing one setpoint).
- Add a newsfragment documenting the export fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/qcodes/dataset/exporters/export_to_xarray.py |
Unstacks conflicting multi_index sub-datasets to avoid xr.merge alignment failures. |
tests/dataset/test_dataset_export.py |
Adds regression coverage ensuring export succeeds and preserves values with NaN padding for off-grid entries. |
docs/changes/newsfragments/8232.improved |
Documents the fixed AlignmentError scenario in release notes. |
… xarray When a dataset has two data variables with different setpoints, and one is a non-grid array parameter (using the multi_index path) while another shares a setpoint coordinate as a standalone dimension, xr.merge raised an AlignmentError because the shared coordinate had conflicting Index objects in the two sub-datasets. Fix: detect this conflict in load_to_xarray_dataset and unstack the multi_index into proper independent dimensions before merging, allowing the shared coordinate to be consistent across all sub-datasets. Fixes #8232 Co-authored-by: jenshnielsen <548266+jenshnielsen@users.noreply.github.com>
Only run the multi_index conflict detection when there are at least two sub-datasets, since with a single variable there is nothing to merge and hence no possible conflict. The existing `standalone_dims` check already handles the case where all variables share multi_index, but add a clear comment explaining both short-circuit conditions. Co-authored-by: jenshnielsen <548266+jenshnielsen@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
jenshnielsen
force-pushed
the
copilot/export-to-xarray-dataset-fix
branch
from
July 31, 2026 13:34
67b4087 to
4d59746
Compare
jenshnielsen
marked this pull request as ready for review
July 31, 2026 13:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DataSet.to_xarray_dataset()raises anAlignmentErrorwhen two data variables share a setpoint coordinate but one of them has additional non-grid setpoints (triggering the multi-index export path).Root cause: Per-parameter sub-datasets are built independently. A non-grid array parameter (e.g.
spectrumwith setpoints(f_stop, freq)where freq values differ per sweep point) gets exported viaxr.Coordinates.from_pandas_multiindex, makingf_stopa component of a MultiIndex rather than a standalone dimension. A co-registered scalar parameter with only(f_stop,)gets a plainPandasIndexforf_stop.xr.mergecannot align these incompatible Index objects.Fix (
export_to_xarray.py):xr.merge, detect sub-datasets that use amulti_indexdimension whose coordinate levels conflict with standalone dimensions in other sub-datasetsds.unstack("multi_index")on the conflicting sub-datasets, converting to regular independent dimensions (NaN-filling off-grid entries) so all sub-datasets share a consistentf_stopcoordinate structureTest (
test_dataset_export.py):f_stopover two values with differing frequency grids, verifying bothspectrum(2-D, NaN-padded) andbar(1-D) are correctly present with values preserved in the merged dataset