Repository navigation
fix: write timezone-aware coordinates in Model.to_netcdf - #1021
Open
MaykThewessen wants to merge 4 commits into
Open
MaykThewessen wants to merge 4 commits into
MaykThewessen wants to merge 4 commits into
Conversation
netCDF has no timezone-aware datetime type, so to_netcdf raised on tz-aware coords (dimension indexes, MultiIndex levels, CSR-backed constraints and data variables in Model.parameters). Write them as naive UTC with the zone names in a `_linopy_tz` attribute and restore them in read_netcdf. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
This was referenced Oct 7, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Auxiliary coordinates with a timezone-aware dtype (the flat-dimension pattern of v1 semantics) still made to_netcdf raise, in dense datasets and in the aux coords of CSR constraints. Handle every non-index variable through one pair of value helpers, and store CSR aux zones as an attribute on the aux variable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 tasks done
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 tasks done
This branch has not been deployed
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.
Note
AI-assisted. Claude wrote the code and tests, and ran the checks listed below. I reviewed the diff.
Changes proposed in this Pull Request
Follow-up to #897. Since that fix a model with timezone-aware coords builds and solves, but
Model.to_netcdf()then raisesTypeError: Cannot interpret 'datetime64[us, Europe/Amsterdam]' as a data type, because netCDF has no timezone-aware datetime type. This also breakssolve(remote=...), which goes throughto_netcdf. PyPSA runs into it as soon as it allows tz-aware snapshots (PyPSA/PyPSA#983).to_netcdfnow writes tz-aware datetimes as naive UTC and stores the zone names in a_linopy_tzattribute, andread_netcdfrestores them. This covers dimension indexes,MultiIndexlevels, auxiliary coordinates (the flat-dimension pattern of v1), CSR-backed constraints including their aux coords, and tz-aware data variables inModel.parameters. Files without the attribute read as before.Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.What was checked (AI-generated)
test_model_to_netcdf_tz_aware_coordsintest/test_io.py, forUTCandEurope/Amsterdamover the 2020-03-29 DST change: flat index, frozen (CSR) constraint andMultiIndexlevel, plus a tz-aware data variable inModel.parameters. It assertsassert_model_equaland that the timezone comes back. All 10 cases fail on master (the 2MultiIndexcases under v1 are skipped, like the existingMultiIndexfixture).test_model_to_netcdf_tz_aware_aux_coords(dense and frozen, 8 cases): all fail on master with theTypeErrorabove. Dense models already drop aux coords on read, also with naive datetimes; that is unchanged here.test_highs_keyboard_interrupttests that failed on the master run only (flaky). The branch has no failures that master lacks; the rest come from the environment (missingoetcextras, HiGHS without SOS).n.model.to_netcdf()thenread_netcdf()gives back equal snapshot indexes with the zone and an equal solution.MultiIndexdimension already fails on master (Grid.from_dataset,TypeError: Must pass list-like as names), so the test leaves that combination out.🤖 Generated with Claude Code