Skip to content

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.

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.

@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