Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accounting for NoneType coordinates in attrs/encoding #359

Merged
merged 3 commits into from
Aug 15, 2022

Conversation

sol1105
Copy link
Contributor

@sol1105 sol1105 commented Aug 15, 2022

With pydata/xarray#5514 it has become practice (at least for me) to occasionally disable the coordinates attribute to be written to file by setting ds['var'].encoding['coordinates'] = None. If used too generously, this may however lead to an AttributeError in the cf-xarray accessor:

   if "coordinates" in obj.encoding:
      search_in.update(obj.encoding["coordinates"].split(" "))

E AttributeError: 'NoneType' object has no attribute 'split'

I suggest this small update to keep that from happening.

cf_xarray/accessor.py Outdated Show resolved Hide resolved
search_in.update(obj.encoding["coordinates"].split(" "))
if "coordinates" in obj.attrs:
search_in.update(obj.attrs["coordinates"].split(" "))
coordinates = obj.encoding.get("coordinates", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
coordinates = obj.encoding.get("coordinates", None)
attrs_or_encoding = ChainMap(self._obj[name].attrs, self._obj[name].encoding)
coordinates = attrs_or_encoding.get("coordinates", None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure if it was intentional that here a set of attrs["coordinates"] and encoding["coordinates"] is being constructed and not the ChainMap operator is being used (which respects then only the first mention of "coordinates" in attrs and encoding).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch. I think we should treat coordinates in both attrs and encoding as a bug perhaps.

cf_xarray/accessor.py Outdated Show resolved Hide resolved
@dcherian dcherian merged commit 92ae91c into xarray-contrib:main Aug 15, 2022
@sol1105 sol1105 deleted the NoneType_coordinates branch August 16, 2022 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants