Skip to content

rename_like warns about conflicting variables #183

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

Merged
merged 9 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,12 +1340,41 @@ def rename_like(
theirkeys = other.cf.keys()

good_keys = ourkeys & theirkeys
renamer = {}
keydict = {}
for key in good_keys:
ours = _single(_get_all)(self._obj, key)[0]
theirs = _single(_get_all)(other, key)[0]
renamer[ours] = theirs
ours = _get_all(self._obj, key)
theirs = _get_all(other, key)
keydict[key] = dict(ours=ours, theirs=theirs)

conflicts = {}
for k0, v0 in keydict.items():
if len(v0["ours"]) > 1 or len(v0["theirs"]) > 1:
conflicts[k0] = v0
continue
for v1 in keydict.values():
# Conflicts have same ours but different theirs or vice versa
if sum([v0["ours"] == v1["ours"], v0["theirs"] == v1["theirs"]]) == 1:
conflicts[k0] = v0
break
if conflicts:
warnings.warn(
"Conflicting variables skipped:\n"
+ "\n".join(
[
f"{sorted(v['ours'])}: {sorted(v['theirs'])} ({k})"
for k, v in sorted(
conflicts.items(), key=lambda item: sorted(item[1]["ours"])
)
]
),
UserWarning,
)

renamer = {
v["ours"][0]: v["theirs"][0]
for k, v in keydict.items()
if k not in conflicts
}
newobj = self._obj.rename(renamer)

# rename variable names in the coordinates attribute
Expand Down
4 changes: 4 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def test_rename_like():
assert "temp" not in renamed
assert "TEMP" in renamed

# skip conflicting variables
with pytest.warns(UserWarning, match="Conflicting variables skipped:.*"):
popds.cf.rename_like(airds)


@pytest.mark.parametrize("obj", objects)
@pytest.mark.parametrize(
Expand Down
31 changes: 25 additions & 6 deletions doc/examples/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,31 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Feature: Renaming coordinate variables\n",
"## Feature: Renaming variables\n",
"\n",
"`cf_xarray` lets you rewrite coordinate variables in one dataset to like\n",
"variables in another dataset. This can only be done when a one-to-one mapping is\n",
"possible\n",
"`cf_xarray` lets you rewrite variables in one dataset to like variables in\n",
"another dataset.\n",
"\n",
"In this example, `TLONG` and `TLAT` are renamed to `lon` and `lat` i.e. their\n",
"In this example, a one-to-one mapping is not possible and the coordinate\n",
"variables are not renamed.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da = pop.cf[\"TEMP\"]\n",
"da.cf.rename_like(ds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we drop the `X` and `Y` axes, a one-to-one mapping is possible. In this\n",
"example, `TLONG` and `TLAT` are renamed to `lon` and `lat` i.e. their\n",
"counterparts in `ds`. Note the the `coordinates` attribute is appropriately\n",
"changed.\n"
]
Expand All @@ -604,7 +622,8 @@
},
"outputs": [],
"source": [
"pop.cf[\"TEMP\"].cf.rename_like(ds)"
"da = da.cf.drop_vars([\"X\", \"Y\"])\n",
"da.cf.rename_like(ds)"
]
},
{
Expand Down