Skip to content

Commit d7f0c15

Browse files
authored
Do not get bounds when extracting a DataArray (#164)
1 parent b36645f commit d7f0c15

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

cf_xarray/accessor.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
List,
1313
Mapping,
1414
MutableMapping,
15+
Optional,
1516
Set,
1617
Tuple,
1718
Union,
@@ -608,10 +609,9 @@ def check_results(names, k):
608609

609610
try:
610611
for name in allnames:
611-
extravars = accessor.get_associated_variable_names(name)
612-
# we cannot return bounds variables with scalar keys
613-
if scalar_key:
614-
extravars.pop("bounds")
612+
extravars = accessor.get_associated_variable_names(
613+
name, skip_bounds=scalar_key
614+
)
615615
coords.extend(itertools.chain(*extravars.values()))
616616

617617
if isinstance(obj, DataArray):
@@ -1209,7 +1209,9 @@ def standard_names(self) -> Dict[str, List[str]]:
12091209

12101210
return {k: sorted(v) for k, v in vardict.items()}
12111211

1212-
def get_associated_variable_names(self, name: Hashable) -> Dict[str, List[str]]:
1212+
def get_associated_variable_names(
1213+
self, name: Hashable, skip_bounds: Optional[bool] = None
1214+
) -> Dict[str, List[str]]:
12131215
"""
12141216
Returns a dict mapping
12151217
1. "ancillary_variables"
@@ -1223,6 +1225,8 @@ def get_associated_variable_names(self, name: Hashable) -> Dict[str, List[str]]:
12231225
12241226
name: Hashable
12251227
1228+
skip_bounds: bool, optional
1229+
12261230
Returns
12271231
------
12281232
@@ -1248,13 +1252,13 @@ def get_associated_variable_names(self, name: Hashable) -> Dict[str, List[str]]:
12481252
"ancillary_variables"
12491253
].split(" ")
12501254

1251-
if "bounds" in attrs_or_encoding:
1252-
coords["bounds"] = [attrs_or_encoding["bounds"]]
1253-
1254-
for dim in self._obj[name].dims:
1255-
dbounds = self._obj[dim].attrs.get("bounds", None)
1256-
if dbounds:
1257-
coords["bounds"].append(dbounds)
1255+
if not skip_bounds:
1256+
if "bounds" in attrs_or_encoding:
1257+
coords["bounds"] = [attrs_or_encoding["bounds"]]
1258+
for dim in self._obj[name].dims:
1259+
dbounds = self._obj[dim].attrs.get("bounds", None)
1260+
if dbounds:
1261+
coords["bounds"].append(dbounds)
12581262

12591263
allvars = itertools.chain(*coords.values())
12601264
missing = set(allvars) - set(self._maybe_to_dataset().variables)

cf_xarray/tests/test_accessor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,15 @@ def test_bounds():
577577
expected = ds["lat_bounds"]
578578
assert_identical(actual, expected)
579579

580+
# Do not attempt to get bounds when extracting a DataArray
581+
# raise a warning when extracting a Dataset and bounds do not exists
582+
ds["time"].attrs["bounds"] = "foo"
583+
with pytest.warns(None) as record:
584+
ds.cf["air"]
585+
assert len(record) == 0
586+
with pytest.warns(UserWarning, match="{'foo'} not found in object"):
587+
ds.cf[["air"]]
588+
580589

581590
def test_bounds_to_vertices():
582591
# All available

0 commit comments

Comments
 (0)