Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize FillValue and missing_value during encoding and decoding steps #3502

Merged
merged 7 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ New Features

Bug fixes
~~~~~~~~~
- Harmonize `_FillValue`, `missing_value` during encoding and decoding steps. (:pull:`3502`)
By `Anderson Banihirwe <https://github.com/andersy005>`_.
- Fix regression introduced in v0.14.0 that would cause a crash if dask is installed
but cloudpickle isn't (:issue:`3401`) by `Rhys Doyle <https://github.com/rdoyle45>`_
- Fix grouping over variables with NaNs. (:issue:`2383`, :pull:`3406`).
Expand Down
14 changes: 10 additions & 4 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from ..core import dtypes, duck_array_ops, indexing
from ..core.pycompat import dask_array_type
from ..core.utils import equivalent
from ..core.variable import Variable


Expand Down Expand Up @@ -152,18 +151,25 @@ def encode(self, variable, name=None):
fv = encoding.get("_FillValue")
mv = encoding.get("missing_value")

if fv is not None and mv is not None and not equivalent(fv, mv):
if (
fv is not None
and mv is not None
and not duck_array_ops.allclose_or_equiv(fv, mv)
):
raise ValueError(
"Variable {!r} has multiple fill values {}. "
"Cannot encode data. ".format(name, [fv, mv])
f"Variable {name!r} has conflicting _FillValue ({fv}) and missing_value ({mv}). Cannot encode data."
)

if fv is not None:
# Ensure _FillValue is cast to same dtype as data's
encoding["_FillValue"] = data.dtype.type(fv)
fill_value = pop_to(encoding, attrs, "_FillValue", name=name)
if not pd.isnull(fill_value):
data = duck_array_ops.fillna(data, fill_value)

if mv is not None:
# Ensure missing_value is cast to same dtype as data's
encoding["missing_value"] = data.dtype.type(mv)
fill_value = pop_to(encoding, attrs, "missing_value", name=name)
if not pd.isnull(fill_value) and fv is None:
data = duck_array_ops.fillna(data, fill_value)
Expand Down
17 changes: 17 additions & 0 deletions xarray/tests/test_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ def test_CFMaskCoder_decode():
assert_identical(expected, encoded)


def test_CFMaskCoder_encode_missing_fill_values_conflict():
original = xr.Variable(
("x",),
[0.0, -1.0, 1.0],
encoding={"_FillValue": np.float32(1e20), "missing_value": np.float64(1e20)},
)
coder = variables.CFMaskCoder()
encoded = coder.encode(original)

assert encoded.dtype == encoded.attrs["missing_value"].dtype
assert encoded.dtype == encoded.attrs["_FillValue"].dtype

with pytest.warns(variables.SerializationWarning):
roundtripped = coder.decode(coder.encode(original))
assert_identical(roundtripped, original)


def test_CFMaskCoder_missing_value():
expected = xr.DataArray(
np.array([[26915, 27755, -9999, 27705], [25595, -9999, 28315, -9999]]),
Expand Down