Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify get_axis_num #8330

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,27 +597,25 @@ 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 : tuple of str
Dimension name(s) for which to lookup axes.

Returns
-------
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}")

Expand Down Expand Up @@ -710,7 +708,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(
Expand Down
Loading