|
8 | 8 |
|
9 | 9 | import numpy as np
|
10 | 10 | import pandas as pd
|
11 |
| -from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined] |
12 | 11 | from packaging.version import Version
|
13 | 12 |
|
14 | 13 | try:
|
|
25 | 24 | dask_array_type = () # type: ignore[assignment, misc]
|
26 | 25 |
|
27 | 26 |
|
| 27 | +def module_available(module: str, minversion: Optional[str] = None) -> bool: |
| 28 | + """Checks whether a module is installed without importing it. |
| 29 | +
|
| 30 | + Use this for a lightweight check and lazy imports. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + module : str |
| 35 | + Name of the module. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + available : bool |
| 40 | + Whether the module is installed. |
| 41 | + """ |
| 42 | + has = importlib.util.find_spec(module) is not None |
| 43 | + if has: |
| 44 | + mod = importlib.import_module(module) |
| 45 | + return Version(mod.__version__) >= Version(minversion) if minversion is not None else True |
| 46 | + else: |
| 47 | + return False |
| 48 | + |
| 49 | + |
| 50 | +if module_available("numpy", minversion="2.0.0"): |
| 51 | + from numpy.lib.array_utils import ( # type: ignore[import-not-found] |
| 52 | + normalize_axis_index, |
| 53 | + ) |
| 54 | +else: |
| 55 | + from numpy.core.numeric import normalize_axis_index # type: ignore[attr-defined] |
| 56 | + |
| 57 | + |
28 | 58 | def asarray(data, xp=np):
|
29 | 59 | return data if is_duck_array(data) else xp.asarray(data)
|
30 | 60 |
|
@@ -349,26 +379,3 @@ def nanlast(values, axis, keepdims=False):
|
349 | 379 | return np.expand_dims(result, axis=axis)
|
350 | 380 | else:
|
351 | 381 | return result
|
352 |
| - |
353 |
| - |
354 |
| -def module_available(module: str, minversion: Optional[str] = None) -> bool: |
355 |
| - """Checks whether a module is installed without importing it. |
356 |
| -
|
357 |
| - Use this for a lightweight check and lazy imports. |
358 |
| -
|
359 |
| - Parameters |
360 |
| - ---------- |
361 |
| - module : str |
362 |
| - Name of the module. |
363 |
| -
|
364 |
| - Returns |
365 |
| - ------- |
366 |
| - available : bool |
367 |
| - Whether the module is installed. |
368 |
| - """ |
369 |
| - has = importlib.util.find_spec(module) is not None |
370 |
| - if has: |
371 |
| - mod = importlib.import_module(module) |
372 |
| - return Version(mod.__version__) >= Version(minversion) if minversion is not None else True |
373 |
| - else: |
374 |
| - return False |
|
0 commit comments