CF accessor adds grid mapping as coordinate

Open
#513 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
28/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
data

Research direction

Start by running the minimal example and tracing the ds.cf[standard_name] accessor alongside Dataset.assign(). Determine whether adding the grid-mapping variable as a coordinate is intended; done should include an agreed behavior for coordinate handling that avoids the shown MergeError, with a regression check for the example.

Written by the indexing model from the issue text.

Description

Possibly a duplicate of #357 or a discussion topic. Opinions welcomed.

Description

Since cf_xarray == 0.8.0 (#391), selecting variables by standard name with ds.cf[standard_name] results in a different set of coordinates than selecting them by their short name with ds[short_name]. This prevents re-injecting these variables in a compatible (and even the original) dataset using Dataset.assign() (and possibly other xarray merge operations). Instead trying to Dataset.assign() a cf-picked, georeferenced variable into a raises a MergeError.

Could you help me to understand if this is a design choice or a bug? In my case it comes as a breaking change and I am not exactly sure how to go about (as a cf-xarray fix or downstream in https://github.com/juseg/hyoga/issues/73). Happy to contribute as I can.

Miminal example
import xarray as xr
import cf_xarray

# make grid-mapped fake data (or use xr.open_dataset)
ds = xr.Dataset({
    'tas': ((), None, {
        'grid_mapping': 'crs',
        'standard_name': 'air_temperature'}),
    'crs': ((), None, {
        'grid_mapping_name': 'latitude_longitude'})})

# ds and ds.cf variables have different coordinates
print('crs' in ds['tas'].coords)  # False
print('crs' in ds.cf['air_temperature'].coords)  # True

# assign a cf-picked variable (only works on cf_xarray < 0.8.0)
ds.assign(new=ds.cf['air_temperature'])  # xarray.core.merge.MergeError
Error message
Traceback (most recent call last):
  File "/home/julien/git/code/hyoga/bugs/cfxr08.py", line 17, in <module>
    ds.assign(new=ds.cf['air_temperature'])  # xarray.core.merge.MergeError
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/xarray/core/dataset.py", line 6080, in assign
    data.update(results)
  File "/usr/lib/python3.12/site-packages/xarray/core/dataset.py", line 4946, in update
    merge_result = dataset_update_method(self, other)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/xarray/core/merge.py", line 1104, in dataset_update_method
    return merge_core(
           ^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/xarray/core/merge.py", line 772, in merge_core
    raise MergeError(
xarray.core.merge.MergeError: unable to determine if these variables should be coordinates or not in the merged result: {'crs'}
Dominant language
Python
Stars
181
Forks
49
Avg merge
43m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xarray-contrib/cf-xarray

All issues in xarray-contrib/cf-xarray

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.