Skip to content

Commit

Permalink
import normalize_axis_index from numpy.lib on numpy>=2 (#364)
Browse files Browse the repository at this point in the history
* import `normalize_axis_index` from `numpy.lib` on `numpy>=2`

* import the right thing
  • Loading branch information
keewis authored May 2, 2024
1 parent 0083ab2 commit fb180e8
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions flox/xrutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import numpy as np
import pandas as pd
from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined]
from packaging.version import Version

try:
Expand All @@ -25,6 +24,37 @@
dask_array_type = () # type: ignore[assignment, misc]


def module_available(module: str, minversion: Optional[str] = None) -> bool:
"""Checks whether a module is installed without importing it.
Use this for a lightweight check and lazy imports.
Parameters
----------
module : str
Name of the module.
Returns
-------
available : bool
Whether the module is installed.
"""
has = importlib.util.find_spec(module) is not None
if has:
mod = importlib.import_module(module)
return Version(mod.__version__) >= Version(minversion) if minversion is not None else True
else:
return False


if module_available("numpy", minversion="2.0.0"):
from numpy.lib.array_utils import ( # type: ignore[import-not-found]
normalize_axis_index,
)
else:
from numpy.core.numeric import normalize_axis_index # type: ignore[attr-defined]


def asarray(data, xp=np):
return data if is_duck_array(data) else xp.asarray(data)

Expand Down Expand Up @@ -349,26 +379,3 @@ def nanlast(values, axis, keepdims=False):
return np.expand_dims(result, axis=axis)
else:
return result


def module_available(module: str, minversion: Optional[str] = None) -> bool:
"""Checks whether a module is installed without importing it.
Use this for a lightweight check and lazy imports.
Parameters
----------
module : str
Name of the module.
Returns
-------
available : bool
Whether the module is installed.
"""
has = importlib.util.find_spec(module) is not None
if has:
mod = importlib.import_module(module)
return Version(mod.__version__) >= Version(minversion) if minversion is not None else True
else:
return False

0 comments on commit fb180e8

Please sign in to comment.