MCPcopy
hub / github.com/pydata/xarray / _getitem_with_mask

Method _getitem_with_mask

xarray/core/variable.py:841–884  ·  view source on GitHub ↗

Index this Variable with -1 remapped to fill_value.

(self, key, fill_value=dtypes.NA)

Source from the content-addressed store, hash-verified

839 return self._replace(dims=dims, data=data)
840
841 def _getitem_with_mask(self, key, fill_value=dtypes.NA):
842 """Index this Variable with -1 remapped to fill_value."""
843 # TODO(shoyer): expose this method in public API somewhere (isel?) and
844 # use it for reindex.
845 # TODO(shoyer): add a sanity check that all other integers are
846 # non-negative
847 # TODO(shoyer): add an optimization, remapping -1 to an adjacent value
848 # that is actually indexed rather than mapping it to the last value
849 # along each axis.
850
851 if fill_value is dtypes.NA:
852 fill_value = dtypes.get_fill_value(self.dtype)
853
854 dims, indexer, new_order = self._broadcast_indexes(key)
855
856 if self.size:
857 if is_duck_dask_array(self._data):
858 # dask's indexing is faster this way; also vindex does not
859 # support negative indices yet:
860 # https://github.com/dask/dask/pull/2967
861 actual_indexer = indexing.posify_mask_indexer(indexer)
862 else:
863 actual_indexer = indexer
864
865 indexable = as_indexable(self._data)
866 data = indexing.apply_indexer(indexable, actual_indexer)
867
868 mask = indexing.create_mask(indexer, self.shape, data)
869 # we need to invert the mask in order to pass data first. This helps
870 # pint to choose the correct unit
871 # TODO: revert after https://github.com/hgrecco/pint/issues/1019 is fixed
872 mask = to_like_array(mask, data)
873 data = duck_array_ops.where(
874 duck_array_ops.logical_not(mask), data, fill_value
875 )
876 else:
877 # array cannot be indexed along dimensions of size 0, so just
878 # build the mask directly instead.
879 mask = indexing.create_mask(indexer, self.shape)
880 data = duck_array_ops.broadcast_to(fill_value, getattr(mask, "shape", ()))
881
882 if new_order:
883 data = duck_array_ops.moveaxis(data, range(len(new_order)), new_order)
884 return self._finalize_indexing_result(dims, data)
885
886 def __setitem__(self, key, value):
887 """__setitem__ is overloaded to access the underlying numpy values with

Calls 7

_broadcast_indexesMethod · 0.95
is_duck_dask_arrayFunction · 0.90
as_indexableFunction · 0.90
to_like_arrayFunction · 0.90
broadcast_toMethod · 0.80
whereMethod · 0.45