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

Function is_empty

dask/utils.py:2325–2362  ·  view source on GitHub ↗

Duck-typed check for “emptiness” of an object. Works for standard sequences (lists, tuples, etc.), NumPy arrays, and sparse-like objects (e.g., SciPy sparse arrays). The function checks: 1. If the object supports len(), returns True if len(obj) == 0. 2. If the obje

(obj)

Source from the content-addressed store, hash-verified

2323
2324
2325def is_empty(obj):
2326 """
2327 Duck-typed check for “emptiness” of an object.
2328
2329 Works for standard sequences (lists, tuples, etc.), NumPy arrays,
2330 and sparse-like objects (e.g., SciPy sparse arrays).
2331
2332 The function checks:
2333 1. If the object supports len(), returns True if len(obj) == 0.
2334 2. If the object has a `.nnz` attribute (number of non-zero elements),
2335 returns True if `.nnz == 0`.
2336 3. If the object has a `.shape` attribute, returns True if any
2337 dimension is zero.
2338 4. Otherwise, returns False (assumes non-empty).
2339
2340 Parameters
2341 ----------
2342 obj : any
2343 The object to check for emptiness.
2344
2345 Returns
2346 -------
2347 bool
2348 True if the object is considered empty, False otherwise.
2349 """
2350 # Check standard sequences
2351 with contextlib.suppress(Exception):
2352 return len(obj) == 0
2353
2354 # Sparse-like objects
2355 with contextlib.suppress(Exception):
2356 return obj.nnz == 0
2357
2358 with contextlib.suppress(Exception):
2359 return 0 in obj.shape
2360
2361 # Fallback: assume non-empty
2362 return False

Calls

no outgoing calls