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
)
| 1564 | |
| 1565 | |
| 1566 | def 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 | |
| 1613 | def _posify_mask_subindexer( |
nothing calls this directly
no test coverage detected
searching dependent graphs…