MCPcopy
hub / github.com/dask/dask / is_arraylike

Function is_arraylike

dask/utils.py:1470–1510  ·  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

1468
1469
1470def 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
1513def 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
sanitize_indexFunction · 0.90
slice_wrap_listsFunction · 0.90
partition_by_sizeFunction · 0.90
normalize_indexFunction · 0.90
check_indexFunction · 0.90

Calls 3

is_dask_collectionFunction · 0.90
anyFunction · 0.85
typenameFunction · 0.85

Tested by 1

test_is_arraylikeFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…