Skip to content

Commit 4524510

Browse files
committed
Bugfix __getitem__
We need to reset_coords to drop any coordinate variables that xarray will automatically bring along. This works for the POP case. I can see a valid use case where we may want to bring some other non-dimensional cooordinate variables that are not referenced in any attrs (i.e. xarray's default behaviour). It seems sensible to expect users to encode that information in an ancillary_variables attribute.
1 parent 0d60420 commit 4524510

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

cf_xarray/accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ def __getitem__(self, key: Union[str, List[str]]):
789789
ds = self._obj
790790

791791
if scalar_key and len(varnames) == 1:
792-
da = ds[varnames[0]]
792+
da = ds[varnames[0]].reset_coords(drop=True)
793793
failed = []
794794
for k1 in coords:
795795
if k1 not in ds.variables:

cf_xarray/tests/test_accessor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ def test_getitem_ancillary_variables():
8989
with pytest.warns(UserWarning):
9090
anc[["q"]].cf["q"]
9191

92+
for k in ["ULONG", "ULAT"]:
93+
assert k not in popds.cf["TEMP"].coords
94+
95+
for k in ["TLONG", "TLAT"]:
96+
assert k not in popds.cf["UVEL"].coords
97+
9298

9399
@pytest.mark.parametrize("obj", objects)
94100
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)