Skip to content

Commit be8b23e

Browse files
authored
add skip to rename_like (#206)
1 parent 9e86af8 commit be8b23e

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

cf_xarray/accessor.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,9 @@ def _maybe_to_dataarray(self, obj=None):
12611261
return obj
12621262

12631263
def rename_like(
1264-
self, other: Union[DataArray, Dataset]
1264+
self,
1265+
other: Union[DataArray, Dataset],
1266+
skip: Union[str, Iterable[str]] = None,
12651267
) -> Union[DataArray, Dataset]:
12661268
"""
12671269
Renames variables in object to match names of like-variables in ``other``.
@@ -1277,20 +1279,30 @@ def rename_like(
12771279
----------
12781280
other : DataArray, Dataset
12791281
Variables will be renamed to match variable names in this xarray object
1282+
skip: str, Iterable[str], optional
1283+
Limit the renaming excluding
1284+
("axes", "cell_measures", "coordinates", "standard_names")
1285+
or a subset thereof.
12801286
12811287
Returns
12821288
-------
12831289
DataArray or Dataset with renamed variables
12841290
"""
1291+
skip = [skip] if isinstance(skip, str) else skip or []
1292+
12851293
ourkeys = self.keys()
12861294
theirkeys = other.cf.keys()
12871295

12881296
good_keys = ourkeys & theirkeys
12891297
keydict = {}
12901298
for key in good_keys:
1291-
ours = _get_all(self._obj, key)
1292-
theirs = _get_all(other, key)
1293-
keydict[key] = dict(ours=ours, theirs=theirs)
1299+
ours = set(_get_all(self._obj, key))
1300+
theirs = set(_get_all(other, key))
1301+
for attr in skip:
1302+
ours -= set(getattr(self, attr).get(key, []))
1303+
theirs -= set(getattr(other.cf, attr).get(key, []))
1304+
if ours and theirs:
1305+
keydict[key] = dict(ours=list(ours), theirs=list(theirs))
12941306

12951307
conflicts = {}
12961308
for k0, v0 in keydict.items():
@@ -1299,7 +1311,7 @@ def rename_like(
12991311
continue
13001312
for v1 in keydict.values():
13011313
# Conflicts have same ours but different theirs or vice versa
1302-
if sum([v0["ours"] == v1["ours"], v0["theirs"] == v1["theirs"]]) == 1:
1314+
if (v0["ours"] == v1["ours"]) != (v0["theirs"] == v1["theirs"]):
13031315
conflicts[k0] = v0
13041316
break
13051317
if conflicts:

cf_xarray/tests/test_accessor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,14 @@ def test_rename_like():
250250
assert "TEMP" in renamed
251251

252252
# skip conflicting variables
253+
da = popds.cf["TEMP"]
253254
with pytest.warns(UserWarning, match="Conflicting variables skipped:.*"):
254-
popds.cf.rename_like(airds)
255+
expected = {"longitude": ["TLONG"], "latitude": ["TLAT"]}
256+
actual = da.cf.rename_like(airds).cf.coordinates
257+
assert expected == actual
258+
expected = {"longitude": ["lon"], "latitude": ["lat"]}
259+
actual = da.cf.rename_like(airds, skip="axes").cf.coordinates
260+
assert expected == actual
255261

256262

257263
@pytest.mark.parametrize("obj", objects)

doc/examples/introduction.ipynb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,10 @@
605605
"cell_type": "markdown",
606606
"metadata": {},
607607
"source": [
608-
"If we drop the `X` and `Y` axes, a one-to-one mapping is possible. In this\n",
609-
"example, `TLONG` and `TLAT` are renamed to `lon` and `lat` i.e. their\n",
610-
"counterparts in `ds`. Note the the `coordinates` attribute is appropriately\n",
611-
"changed.\n"
608+
"If we exclude all axes (variables with `axis` attribute), a one-to-one mapping\n",
609+
"is possible. In this example, `TLONG` and `TLAT` are renamed to `lon` and `lat`\n",
610+
"i.e. their counterparts in `ds`. Note the the `coordinates` attribute is\n",
611+
"appropriately changed.\n"
612612
]
613613
},
614614
{
@@ -622,8 +622,7 @@
622622
},
623623
"outputs": [],
624624
"source": [
625-
"da = da.cf.drop_vars([\"X\", \"Y\"])\n",
626-
"da.cf.rename_like(ds)"
625+
"da.cf.rename_like(ds, skip=\"axes\")"
627626
]
628627
},
629628
{

0 commit comments

Comments
 (0)