From b96b577da1d07a608a2d89339ca7e286874309f6 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:15:14 +0200 Subject: [PATCH 1/2] Update core.py --- xarray/namedarray/core.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index 6833215a9f2..c2c77de1f5e 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -330,12 +330,12 @@ def _dask_finalize( data = array_func(results, *args, **kwargs) return type(self)(self._dims, data, attrs=self._attrs) - def get_axis_num(self, dim: Hashable | Iterable[Hashable]) -> int | tuple[int, ...]: + def _get_axis_nums(self, dims: _Dims) -> tuple[int, ...]: """Return axis number(s) corresponding to dimension(s) in this array. Parameters ---------- - dim : str or iterable of str + dim : iterable of str Dimension name(s) for which to lookup axes. Returns @@ -343,14 +343,12 @@ def get_axis_num(self, dim: Hashable | Iterable[Hashable]) -> int | tuple[int, . int or tuple of int Axis number or numbers corresponding to the given dimensions. """ - if not isinstance(dim, str) and isinstance(dim, Iterable): - return tuple(self._get_axis_num(d) for d in dim) - else: - return self._get_axis_num(dim) + return tuple(self._get_axis_num(d) for d in dims) - def _get_axis_num(self: Any, dim: Hashable) -> int: + def _get_axis_num(self: Any, dim: _Dim) -> int: try: - return self.dims.index(dim) # type: ignore[no-any-return] + out: int = self.dims.index(dim) + return out except ValueError: raise ValueError(f"{dim!r} not found in array dimensions {self.dims!r}") @@ -515,7 +513,7 @@ def reduce( raise ValueError("cannot supply both 'axis' and 'dim' arguments") if dim is not None: - axis = self.get_axis_num(dim) + axis = self._get_axis_nums(dim) with warnings.catch_warnings(): warnings.filterwarnings( From 99784b6a61c71481122f942091f4b931083ceb76 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:17:13 +0200 Subject: [PATCH 2/2] Update core.py --- xarray/namedarray/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index c2c77de1f5e..1f92c24c791 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -335,7 +335,7 @@ def _get_axis_nums(self, dims: _Dims) -> tuple[int, ...]: Parameters ---------- - dim : iterable of str + dim : tuple of str Dimension name(s) for which to lookup axes. Returns