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 4 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
11 changes: 11 additions & 0 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,17 @@ def rename_like(
theirs = _single(_get_all)(other, key)[0]
renamer[ours] = theirs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there weird cases where this is actually a conflict and we should skip and warn about it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea ;) lets find out.


conflicts = {
k: v for k, v in renamer.items() if list(renamer.values()).count(v) > 1
}
if conflicts:
renamer = {k: v for k, v in renamer.items() if k not in conflicts}
warnings.warn(
"Conflicting variables skipped:\n"
+ "\n".join([f"{k}: {conflicts[k]}" for k in sorted(conflicts)]),
UserWarning,
)

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["TEMP"].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