Skip to content

Use numpy's normalize_axis_index, if available #1679

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

Closed
wants to merge 1 commit 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
23 changes: 14 additions & 9 deletions xarray/core/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import pandas as pd
import warnings


def _validate_axis(data, axis):
ndim = data.ndim
if not -ndim <= axis < ndim:
raise IndexError('axis %r out of bounds [-%r, %r)'
% (axis, ndim, ndim))
if axis < 0:
axis += ndim
return axis
# Numpy has a function for this as of 1.13
_normalize_axis_index = getattr(np.core.multiarray, 'normalize_axis_index', None)
if _normalize_axis_index is not None:
def _validate_axis(data, axis):
return _normalize_axis_index(axis, data.ndim)
else:
def _validate_axis(data, axis):
ndim = data.ndim
if not -ndim <= axis < ndim:
raise IndexError('axis %r out of bounds [-%r, %r)'
% (axis, ndim, ndim))
if axis < 0:
axis += ndim
return axis


def _select_along_axis(values, idx, axis):
Expand Down