MCPcopy Create free account
hub / github.com/dask/dask / is_arraylike

Function is_arraylike

dask/utils.py:1473–1513  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

make_meta_objectFunction · 0.90
_tree_repr_linesMethod · 0.90
_get_meta_map_partitionsFunction · 0.90
_locMethod · 0.90
_get_partitionsFunction · 0.90
to_numericFunction · 0.90
test_is_arraylikeFunction · 0.90
partial_reduceFunction · 0.90
sanitize_indexFunction · 0.90
slice_wrap_listsFunction · 0.90
partition_by_sizeFunction · 0.90
normalize_indexFunction · 0.90

Calls 3

is_dask_collectionFunction · 0.90
anyFunction · 0.85
typenameFunction · 0.85

Tested by 1

test_is_arraylikeFunction · 0.72