Return an ndarray if the given object is implicitly convertible to ndarray, and numpy is already imported, otherwise None.
(obj: object)
| 906 | |
| 907 | |
| 908 | def _as_numpy_array(obj: object) -> ndarray | None: |
| 909 | """ |
| 910 | Return an ndarray if the given object is implicitly convertible to ndarray, |
| 911 | and numpy is already imported, otherwise None. |
| 912 | """ |
| 913 | np: Any = sys.modules.get("numpy") |
| 914 | if np is not None: |
| 915 | # avoid infinite recursion on numpy scalars, which have __array__ |
| 916 | if np.isscalar(obj): |
| 917 | return None |
| 918 | elif isinstance(obj, np.ndarray): |
| 919 | return obj |
| 920 | elif hasattr(obj, "__array__") or hasattr(obj, "__array_interface__"): |
| 921 | return np.asarray(obj) |
| 922 | return None |
no test coverage detected