solve a bug when the units attribute is not a string #7085
Merged
TomNicholas merged 8 commits intopydata:mainfrom Sep 28, 2022
Merged
solve a bug when the units attribute is not a string #7085TomNicholas merged 8 commits intopydata:mainfrom
TomNicholas merged 8 commits intopydata:mainfrom
Conversation
Collaborator
|
Looks good to me. |
TomNicholas
approved these changes
Sep 26, 2022
headtr1ck
approved these changes
Sep 26, 2022
andersy005
approved these changes
Sep 27, 2022
dcherian
reviewed
Sep 27, 2022
|
|
||
| @requires_cftime | ||
| def test_scalar_unit() -> None: | ||
| # test that a scalar units (often NaN when using to_netcdf) does not raise an error |
Contributor
There was a problem hiding this comment.
Does Xarray actually write a nan for units? That seems problematic. Can you open a new issue witha reproducible example?
Member
|
Test failures seem unrelated (same failures with pyDAP occurred flakily in main) so I'm going to merge this shortly. Thanks @ghislainp ! |
4 tasks
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.
whats-new.rstapi.rstWe faced a sort of bug with a colleague of mine. It seems to be legal to set a numeric value to the units attributes in an xarray or a netcdf file. xarray accepts to save such an array to netcdf: xr.DataArray([1, 2, 3], attrs={'units': 1}, name='x').to_csv('tmp.nc'). Reading this netcdf file with xarray.open_dataset raises an error.
It is unlikely to have a scalar for the units, but at least it happened to us (the value was NaN) and this raised an exception very difficult to understand.
This raises an exception because
"since" in attrs["units"]was called twice in xarray codebase (in coding_times.py and in conventions.py) without checking for the type of the attribute.This PR solves this improbable bug