Skip to content

More support for missing_value. #2973

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

Merged
merged 8 commits into from
Jun 12, 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 @@ -72,6 +72,8 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian`_.
- A deep copy deep-copies the coords (:issue:`1463`)
By `Martin Pletcher <https://github.com/pletchm>`_.
- Increased support for `missing_value` (:issue:`2871`)
By `Deepak Cherian <https://github.com/dcherian>`_.
- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`)
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Fixed performance issues with cftime installed (:issue:`3000`)
Expand Down
16 changes: 15 additions & 1 deletion xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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 @@ -145,11 +146,24 @@ class CFMaskCoder(VariableCoder):
def encode(self, variable, name=None):
dims, data, attrs, encoding = unpack_for_encoding(variable)

if encoding.get('_FillValue') is not 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):
raise ValueError("Variable {!r} has multiple fill values {}. "
"Cannot encode data. "
.format(name, [fv, mv]))

if fv is not None:
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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch @shoyer . how about this?

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)

return Variable(dims, data, attrs, encoding)

def decode(self, variable, name=None):
Expand Down
3 changes: 2 additions & 1 deletion xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def maybe_encode_nonstring_dtype(var, name=None):
if dtype != var.dtype:
if np.issubdtype(dtype, np.integer):
if (np.issubdtype(var.dtype, np.floating) and
'_FillValue' not in var.attrs):
'_FillValue' not in var.attrs and
'missing_value' not in var.attrs):
warnings.warn('saving variable %s with floating '
'point data as an integer dtype without '
'any _FillValue to use for NaNs' % name,
Expand Down
19 changes: 18 additions & 1 deletion xarray/tests/test_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import xarray as xr
from xarray.coding import variables

from . import assert_identical, requires_dask
from . import assert_equal, assert_identical, requires_dask

with suppress(ImportError):
import dask.array as da
Expand All @@ -20,6 +20,23 @@ def test_CFMaskCoder_decode():
assert_identical(expected, encoded)


def test_CFMaskCoder_missing_value():
expected = xr.DataArray(np.array([[26915, 27755, -9999, 27705],
[25595, -9999, 28315, -9999]]),
dims=['npts', 'ntimes'],
name='tmpk')
expected.attrs['missing_value'] = -9999

decoded = xr.decode_cf(expected.to_dataset())
encoded, _ = xr.conventions.cf_encoder(decoded, decoded.attrs)

assert_equal(encoded['tmpk'], expected.variable)

decoded.tmpk.encoding['_FillValue'] = -9940
with pytest.raises(ValueError):
encoded, _ = xr.conventions.cf_encoder(decoded, decoded.attrs)


@requires_dask
def test_CFMaskCoder_decode_dask():
original = xr.Variable(('x',), [0, -1, 1], {'_FillValue': -1}).chunk()
Expand Down