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

Method encode

xarray/coding/variables.py:629–654  ·  view source on GitHub ↗
(self, variable: Variable, name: T_Name = None)

Source from the content-addressed store, hash-verified

627 """Encode NonString variables if dtypes differ."""
628
629 def encode(self, variable: Variable, name: T_Name = None) -> Variable:
630 if "dtype" in variable.encoding and variable.encoding["dtype"] not in (
631 "S1",
632 str,
633 ):
634 dims, data, attrs, encoding = unpack_for_encoding(variable)
635 dtype = np.dtype(encoding.pop("dtype"))
636 if dtype != variable.dtype:
637 if np.issubdtype(dtype, np.integer):
638 if (
639 np.issubdtype(variable.dtype, np.floating)
640 and "_FillValue" not in variable.attrs
641 and "missing_value" not in variable.attrs
642 ):
643 warnings.warn(
644 f"saving variable {name} with floating "
645 "point data as an integer dtype without "
646 "any _FillValue to use for NaNs",
647 SerializationWarning,
648 stacklevel=10,
649 )
650 data = duck_array_ops.round(data)
651 data = duck_array_ops.astype(data, dtype=dtype)
652 return Variable(dims, data, attrs, encoding, fastpath=True)
653 else:
654 return variable
655
656 def decode(self):
657 raise NotImplementedError()

Callers

nothing calls this directly

Calls 5

unpack_for_encodingFunction · 0.90
VariableClass · 0.90
dtypeMethod · 0.45
roundMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected