Rename a variable name in a DataTree

I am using the xradar package to read gamic radars files
dtree =
Working well

How can I rename a variable name within the datatree ?

First of all, welcome to openradar community @yslak. If you want to tell the community something about you, then just follow up at Introduce yourself to the community!

I’m assuming you are referring to radar moment variables, like DBZH. For a dtree with subgroup sweep_0 this would be:

dtree["sweep_0"] = dtree["sweep_0"].rename({"DBZH": "DBZH_CHANGED"})

This is essentially overwriting that specific Datatree Node.

If you have a DataTree with several subgroups (eg. volume) and you want to do the renaming for all subgroups the following will work:

def rename(ds, mapping):
    # drop names, which are not in current Dataset
    mdict = {k:v for k,v in mapping.items() if k in ds}
    return ds.rename(mdict)

# inplace
dtree.map_over_subtree_inplace(rename, {"DBZH":"DBZH_CHANGED"})

# assign new tree
dtree = dtree.map_over_subtree(rename, {"DBZH":"DBZH_CHANGED"})

If you want to read more about datatree capabilities in particular please head over to As datatree is using much of xarray that is also a good point of reading

Both suggested solutions are working very well for me

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.