From dc78afab1897ca29f9f30d9916f40e9d7e6635f3 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Sat, 1 May 2021 14:28:09 +0100 Subject: [PATCH 1/2] uses rather than --- cf_xarray/accessor.py | 20 ++++++++++++-------- cf_xarray/tests/test_accessor.py | 4 ++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index c0205afb..9aafafd7 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -1586,15 +1586,15 @@ def get_bounds_dim_name(self, key: str) -> str: assert self._obj.sizes[bounds_dim] in [2, 4] return bounds_dim - def add_bounds(self, dims: Union[Hashable, Iterable[Hashable]]): + def add_bounds(self, keys: Union[str, Iterable[str]]): """ Returns a new object with bounds variables. The bounds values are guessed assuming equal spacing on either side of a coordinate label. Parameters ---------- - dims : Hashable or Iterable[Hashable] - Either a single dimension name or a list of dimension names. + keys : str or Iterable[str] + Either a single key or a list of keys corresponding to dimensions. Returns ------- @@ -1609,12 +1609,16 @@ def add_bounds(self, dims: Union[Hashable, Iterable[Hashable]]): The bounds variables are automatically named f"{dim}_bounds" where ``dim`` is a dimension name. """ - if isinstance(dims, Hashable): - dimensions = (dims,) - else: - dimensions = dims + if isinstance(keys, str): + keys = [keys] + + dimensions = set() + for key in keys: + dimensions |= set( + apply_mapper(_get_dims, self._obj, key, error=False, default=[key]) + ) - bad_dims: Set[Hashable] = set(dimensions) - set(self._obj.dims) + bad_dims: Set[str] = dimensions - set(self._obj.dims) if bad_dims: raise ValueError( f"{bad_dims!r} are not dimensions in the underlying object." diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 6f4060d4..dfa5ebc9 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -634,6 +634,10 @@ def test_add_bounds(obj, dims): assert added[dim].attrs["bounds"] == name assert_allclose(added[name].reset_coords(drop=True), expected[dim]) + # Test multiple dimensions + assert not {"x1_bounds", "x2_bounds"} <= set(multiple.variables) + assert {"x1_bounds", "x2_bounds"} <= set(multiple.cf.add_bounds("X").variables) + def test_bounds(): ds = airds.copy(deep=True).cf.add_bounds("lat") From 2f74c30c930f320f23ff0d8864498e99afd8d94d Mon Sep 17 00:00:00 2001 From: malmans2 Date: Mon, 3 May 2021 18:39:16 +0100 Subject: [PATCH 2/2] use update and add what's new --- cf_xarray/accessor.py | 2 +- doc/whats-new.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 9aafafd7..14d7314e 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -1614,7 +1614,7 @@ def add_bounds(self, keys: Union[str, Iterable[str]]): dimensions = set() for key in keys: - dimensions |= set( + dimensions.update( apply_mapper(_get_dims, self._obj, key, error=False, default=[key]) ) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index af1a88be..3e5fae81 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -6,6 +6,7 @@ What's New v0.5.2 (unreleased) =================== +- Replace the ``dims`` argument of :py:meth:`Dataset.cf.add_bounds` with ``keys``, allowing to use CF keys. By `Mattia Almansi`_. - Added :py:attr:`DataArray.cf.formula_terms` and :py:attr:`Dataset.cf.formula_terms`. By `Deepak Cherian`_. - Added :py:attr:`Dataset.cf.bounds` to return a dictionary mapping valid keys to the variable names of their bounds. By `Mattia Almansi`_.