MCPcopy Index your code
hub / github.com/pydata/xarray / create_mask

Function create_mask

xarray/core/indexing.py:1566–1610  ·  view source on GitHub ↗

Create a mask for indexing with a fill-value. Parameters ---------- indexer : ExplicitIndexer Indexer with -1 in integer or ndarray value to indicate locations in the result that should be masked. shape : tuple Shape of the array being indexed. data : opt

(
    indexer: ExplicitIndexer, shape: _Shape, data: duckarray[Any, Any] | None = None
)

Source from the content-addressed store, hash-verified

1564
1565
1566def create_mask(
1567 indexer: ExplicitIndexer, shape: _Shape, data: duckarray[Any, Any] | None = None
1568):
1569 """Create a mask for indexing with a fill-value.
1570
1571 Parameters
1572 ----------
1573 indexer : ExplicitIndexer
1574 Indexer with -1 in integer or ndarray value to indicate locations in
1575 the result that should be masked.
1576 shape : tuple
1577 Shape of the array being indexed.
1578 data : optional
1579 Data for which mask is being created. If data is a dask arrays, its chunks
1580 are used as a hint for chunks on the resulting mask. If data is a sparse
1581 array, the returned mask is also a sparse array.
1582
1583 Returns
1584 -------
1585 mask : bool, np.ndarray, SparseArray or dask.array.Array with dtype=bool
1586 Same type as data. Has the same shape as the indexing result.
1587 """
1588 if isinstance(indexer, OuterIndexer):
1589 key = _outer_to_vectorized_indexer(indexer, shape).tuple
1590 assert not any(isinstance(k, slice) for k in key)
1591 mask = _masked_result_drop_slice(key, data)
1592
1593 elif isinstance(indexer, VectorizedIndexer):
1594 key = indexer.tuple
1595 base_mask = _masked_result_drop_slice(key, data)
1596 slice_shape = tuple(
1597 np.arange(*k.indices(size)).size
1598 for k, size in zip(key, shape, strict=False)
1599 if isinstance(k, slice)
1600 )
1601 expanded_mask = base_mask[(Ellipsis,) + (np.newaxis,) * len(slice_shape)]
1602 mask = duck_array_ops.broadcast_to(expanded_mask, base_mask.shape + slice_shape)
1603
1604 elif isinstance(indexer, BasicIndexer):
1605 mask = any(k == -1 for k in indexer.tuple)
1606
1607 else:
1608 raise TypeError(f"unexpected key type: {type(indexer)}")
1609
1610 return mask
1611
1612
1613def _posify_mask_subindexer(

Callers

nothing calls this directly

Calls 5

typeFunction · 0.85
arangeMethod · 0.80
broadcast_toMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…