Is this object a numpy array or something similar? This function tests specifically for an object that already has array attributes (e.g. np.ndarray, dask.array.Array, cupy.ndarray, sparse.COO), **NOT** for something that can be coerced into an array object (e.g. Python lists and tu
(x)
| 1468 | |
| 1469 | |
| 1470 | def is_arraylike(x) -> bool: |
| 1471 | """Is this object a numpy array or something similar? |
| 1472 | |
| 1473 | This function tests specifically for an object that already has |
| 1474 | array attributes (e.g. np.ndarray, dask.array.Array, cupy.ndarray, |
| 1475 | sparse.COO), **NOT** for something that can be coerced into an |
| 1476 | array object (e.g. Python lists and tuples). It is meant for dask |
| 1477 | developers and developers of downstream libraries. |
| 1478 | |
| 1479 | Note that this function does not correspond with NumPy's |
| 1480 | definition of array_like, which includes any object that can be |
| 1481 | coerced into an array (see definition in the NumPy glossary): |
| 1482 | https://numpy.org/doc/stable/glossary.html |
| 1483 | |
| 1484 | Examples |
| 1485 | -------- |
| 1486 | >>> import numpy as np |
| 1487 | >>> is_arraylike(np.ones(5)) |
| 1488 | True |
| 1489 | >>> is_arraylike(np.ones(())) |
| 1490 | True |
| 1491 | >>> is_arraylike(5) |
| 1492 | False |
| 1493 | >>> is_arraylike('cat') |
| 1494 | False |
| 1495 | """ |
| 1496 | from dask.base import is_dask_collection |
| 1497 | |
| 1498 | is_duck_array = hasattr(x, "__array_function__") or hasattr(x, "__array_ufunc__") |
| 1499 | |
| 1500 | return bool( |
| 1501 | hasattr(x, "shape") |
| 1502 | and isinstance(x.shape, tuple) |
| 1503 | and hasattr(x, "dtype") |
| 1504 | and not any(is_dask_collection(n) for n in x.shape) |
| 1505 | # We special case scipy.sparse and cupyx.scipy.sparse arrays as having partial |
| 1506 | # support for them is useful in scenarios where we mostly call `map_partitions` |
| 1507 | # or `map_blocks` with scikit-learn functions on dask arrays and dask dataframes. |
| 1508 | # https://github.com/dask/dask/pull/3738 |
| 1509 | and (is_duck_array or "scipy.sparse" in typename(type(x))) |
| 1510 | ) |
| 1511 | |
| 1512 | |
| 1513 | def is_dataframe_like(df) -> bool: |
searching dependent graphs…