Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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: 1 addition & 1 deletion xarray/backends/netCDF4_.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False,

safe_to_drop = set(['source', 'original_shape'])
valid_encodings = set(['zlib', 'complevel', 'fletcher32', 'contiguous',
'chunksizes', 'shuffle'])
'chunksizes', 'shuffle', '_FillValue'])
if lsd_okay:
valid_encodings.add('least_significant_digit')

Expand Down
5 changes: 3 additions & 2 deletions xarray/backends/scipy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ def encode_variable(self, variable):
def prepare_variable(self, name, variable, check_encoding=False,
unlimited_dims=None):
if check_encoding and variable.encoding:
raise ValueError('unexpected encoding for scipy backend: %r'
% list(variable.encoding))
if variable.encoding != {'_FillValue': None}:
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is a little awkward.

I think this is probably OK (backend classes already need to know about encodings), but it would be good to think about why we don't already have validation for encodings with scipy. We should probably be raising an error if somebody attempts an encoding like {'zlib': True} but right now it looks like we don't do that.

raise ValueError('unexpected encoding for scipy backend: %r'
% list(variable.encoding))

data = variable.data
# nb. this still creates a numpy array in all memory, even though we
Expand Down
20 changes: 20 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,26 @@ def test_explicitly_omit_fill_value(self):
with self.roundtrip(ds) as actual:
assert '_FillValue' not in actual.x.encoding

def test_explicitly_omit_fill_value_via_encoding_kwarg(self):
ds = Dataset({'x': ('y', [np.pi, -np.pi])})
kwargs = dict(encoding={'x': {'_FillValue': None}})
with self.roundtrip(ds, save_kwargs=kwargs) as actual:
assert '_FillValue' not in actual.x.encoding
self.assertEqual(ds.y.encoding, {})

def test_explicitly_omit_fill_value_in_coord(self):
ds = Dataset({'x': ('y', [np.pi, -np.pi])}, coords={'y': [0.0, 1.0]})
ds.y.encoding['_FillValue'] = None
with self.roundtrip(ds) as actual:
assert '_FillValue' not in actual.y.encoding

def test_explicitly_omit_fill_value_in_coord_via_encoding_kwarg(self):
ds = Dataset({'x': ('y', [np.pi, -np.pi])}, coords={'y': [0.0, 1.0]})
kwargs = dict(encoding={'y': {'_FillValue': None}})
with self.roundtrip(ds, save_kwargs=kwargs) as actual:
assert '_FillValue' not in actual.y.encoding
self.assertEqual(ds.y.encoding, {})

def test_encoding_same_dtype(self):
ds = Dataset({'x': ('y', np.arange(10.0, dtype='f4'))})
kwargs = dict(encoding={'x': {'dtype': 'f4'}})
Expand Down