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

Method _calculate_binary_op

xarray/core/dataset.py:7776–7818  ·  view source on GitHub ↗
(
        self, f, other, join="inner", inplace: bool = False
    )

Source from the content-addressed store, hash-verified

7774 return self
7775
7776 def _calculate_binary_op(
7777 self, f, other, join="inner", inplace: bool = False
7778 ) -> Dataset:
7779 def apply_over_both(lhs_data_vars, rhs_data_vars, lhs_vars, rhs_vars):
7780 if inplace and set(lhs_data_vars) != set(rhs_data_vars):
7781 raise ValueError(
7782 "datasets must have the same data variables "
7783 f"for in-place arithmetic operations: {list(lhs_data_vars)}, {list(rhs_data_vars)}"
7784 )
7785
7786 dest_vars = {}
7787
7788 for k in lhs_data_vars:
7789 if k in rhs_data_vars:
7790 dest_vars[k] = f(lhs_vars[k], rhs_vars[k])
7791 elif join in ["left", "outer"]:
7792 dest_vars[k] = f(lhs_vars[k], np.nan)
7793 for k in rhs_data_vars:
7794 if k not in dest_vars and join in ["right", "outer"]:
7795 dest_vars[k] = f(rhs_vars[k], np.nan)
7796 return dest_vars
7797
7798 if utils.is_dict_like(other) and not isinstance(other, Dataset):
7799 # can't use our shortcut of doing the binary operation with
7800 # Variable objects, so apply over our data vars instead.
7801 new_data_vars = apply_over_both(
7802 self.data_vars, other, self.data_vars, other
7803 )
7804 return type(self)(new_data_vars)
7805
7806 other_coords: Coordinates | None = getattr(other, "coords", None)
7807 ds = self.coords.merge(other_coords, compat=OPTIONS["arithmetic_compat"])
7808
7809 if isinstance(other, Dataset):
7810 new_vars = apply_over_both(
7811 self.data_vars, other.data_vars, self.variables, other.variables
7812 )
7813 else:
7814 other_variable = getattr(other, "variable", other)
7815 new_vars = {k: f(self.variables[k], other_variable) for k in self.data_vars}
7816 ds._variables.update(new_vars)
7817 ds._dims = calculate_dimensions(ds._variables)
7818 return ds
7819
7820 def _copy_attrs_from(self, other):
7821 self.attrs = other.attrs

Callers 2

_binary_opMethod · 0.95
_inplace_binary_opMethod · 0.95

Calls 5

calculate_dimensionsFunction · 0.90
typeFunction · 0.85
fFunction · 0.70
mergeMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected