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

Method _binary_op

xarray/core/groupby.py:883–971  ·  view source on GitHub ↗
(self, other, f, reflexive=False)

Source from the content-addressed store, hash-verified

881 return dim, positions
882
883 def _binary_op(self, other, f, reflexive=False):
884 from xarray.core.dataarray import DataArray
885 from xarray.core.dataset import Dataset
886
887 g = f if not reflexive else lambda x, y: f(y, x)
888
889 self._raise_if_not_single_group()
890 (grouper,) = self.groupers
891 obj = self._original_obj
892 name = grouper.name
893 group = grouper.group
894 codes = self.encoded.codes
895 dims = group.dims
896
897 if isinstance(group, _DummyGroup):
898 group = coord = group.to_dataarray()
899 else:
900 coord = grouper.unique_coord
901 if isinstance(coord, Variable):
902 assert coord.ndim == 1
903 (coord_dim,) = coord.dims
904 # TODO: explicitly create Index here
905 coord = DataArray(coord, coords={coord_dim: coord.data})
906
907 if not isinstance(other, Dataset | DataArray):
908 raise TypeError(
909 "GroupBy objects only support binary ops "
910 "when the other argument is a Dataset or "
911 "DataArray"
912 )
913
914 if name not in other.dims:
915 raise ValueError(
916 "incompatible dimensions for a grouped "
917 f"binary operation: the group variable {name!r} "
918 "is not a dimension on the other argument "
919 f"with dimensions {other.dims!r}"
920 )
921
922 # Broadcast out scalars for backwards compatibility
923 # TODO: get rid of this when fixing GH2145
924 for var in other.coords:
925 if other[var].ndim == 0:
926 other[var] = (
927 other[var].drop_vars(var).expand_dims({name: other.sizes[name]})
928 )
929
930 # need to handle NaNs in group or elements that don't belong to any bins
931 mask = codes == -1
932 if mask.any():
933 obj = obj.where(~mask, drop=True)
934 group = group.where(~mask, drop=True)
935 codes = codes.where(~mask, drop=True).astype(int)
936
937 # if other is dask-backed, that's a hint that the
938 # "expanded" dataset is too big to hold in memory.
939 # this can be the case when `other` was read from disk
940 # and contains our lazy indexing classes

Callers

nothing calls this directly

Calls 15

DataArrayClass · 0.90
alignFunction · 0.90
itemsMethod · 0.80
fFunction · 0.70
to_dataarrayMethod · 0.45
expand_dimsMethod · 0.45
drop_varsMethod · 0.45
anyMethod · 0.45
whereMethod · 0.45
astypeMethod · 0.45
chunkMethod · 0.45

Tested by

no test coverage detected