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 2 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 @@ -41,6 +41,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>`_.

.. _whats-new.0.12.1:

Expand Down
17 changes: 15 additions & 2 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Coders for individual Variable objects."""
from typing import Any
import warnings
from functools import partial
from typing import Any

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -145,11 +145,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 (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):
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): # noqa
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