Repository navigation
fix: write timezone-aware coordinates in Model.to_netcdf - #1021
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
7ed87cc to
adee70b
Compare
|
Thanks @MaykThewessen the feature is useful and we need it for PyPSA/PyPSA#983. But I'd like a simpler approach before merging. The code looks hacky and does not draw the right boundary
Right now the timezone handling lives in three places (
That covers indexes, MultiIndex levels, CSR |
Thanks @FabianHofmann, agreed. I rewrote it along those lines in d38fd4f: two helpers in One detail: on read, a dimension coordinate comes back as a tz-aware Checked by machine: the diff against |
netCDF has no timezone-aware datetime type, so to_netcdf raised on tz-aware indexes, auxiliary coordinates and parameters. Handle it at the netcdf boundary only: _tz_to_naive_utc writes naive UTC with the zone in a _linopy_tz attribute right before ds.to_netcdf, and _tz_from_naive_utc restores it in read_netcdf, auxiliary coordinates included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2636e72 to
6a861d3
Compare
|
thanks @MaykThewessen |
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