2

Is there a way to rename global attribute names using xarray? The rename command only seems to rename variables and dimensions and not global attributes. I tried this:

with util.open_or_die('AA.nc', perm='r+') as hndl_nc:
    hndl_nc.rename({'src_name': 'dst_name'}, inplace=True)

But I get this error:

AttributeError: NetCDF: Attribute not found

1 Answer 1

1

The xarray attrs attribute (which holds the attributes you're accessing) is simply an OrderedDict. There's no method in xarray that explicitly allows this behavior, but the attrs can be modified directly, e.g.:

hndl_nc.attrs['dst_name'] = hndl_nc.attrs.pop('src_name')
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @delgadom! is there a way I can delete an attrs as well? e.g. delete dst_name instead of renaming it?
attrs.pop removes and returns the value of dst_name. So there shouldn't be any value for dst_name after this line. If you just to remove it you could just use the later half of the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.