Skip to content

Commit 33660b7

Browse files
czrothshoyer
authored andcommitted
Add '_FillValue' to set of valid_encodings for netCDF4 backend (#1869)
* Add '_FillValue' to set of valid_encodings for netCDF4 backend * Add additional omit_fill_value tests Add additional tests to prevent future regression of setting _FillValue to None when using the encoding kwarg in to_netcdf. * Fix additional omit_fill_value tests Remove copy/paste line that shouldn't have been in a test. Add additional asserts. Fix indentation. * Fix scipy failure in additional omit_fill_value tests * Added bug-fix documentation for #1865
1 parent 93a4039 commit 33660b7

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Bug fixes
128128
- Compatibility fixes to plotting module for Numpy 1.14 and Pandas 0.22
129129
(:issue:`1813`).
130130
By `Joe Hamman <https://github.com/jhamman>`_.
131+
- Bug fix in encoding coordinates with ``{'_FillValue': None}`` in netCDF
132+
metadata (:issue:`1865`).
133+
By `Chris Roth <https://github.com/czr137>`_.
131134
- Fix indexing with lists for arrays loaded from netCDF files with
132135
``engine='h5netcdf`` (:issue:`1864`).
133136
By `Stephan Hoyer <https://github.com/shoyer>`_.

xarray/backends/netCDF4_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False,
160160

161161
safe_to_drop = set(['source', 'original_shape'])
162162
valid_encodings = set(['zlib', 'complevel', 'fletcher32', 'contiguous',
163-
'chunksizes', 'shuffle'])
163+
'chunksizes', 'shuffle', '_FillValue'])
164164
if lsd_okay:
165165
valid_encodings.add('least_significant_digit')
166166

xarray/backends/scipy_.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ def encode_variable(self, variable):
188188
def prepare_variable(self, name, variable, check_encoding=False,
189189
unlimited_dims=None):
190190
if check_encoding and variable.encoding:
191-
raise ValueError('unexpected encoding for scipy backend: %r'
192-
% list(variable.encoding))
191+
if variable.encoding != {'_FillValue': None}:
192+
raise ValueError('unexpected encoding for scipy backend: %r'
193+
% list(variable.encoding))
193194

194195
data = variable.data
195196
# nb. this still creates a numpy array in all memory, even though we

xarray/tests/test_backends.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,26 @@ def test_explicitly_omit_fill_value(self):
704704
with self.roundtrip(ds) as actual:
705705
assert '_FillValue' not in actual.x.encoding
706706

707+
def test_explicitly_omit_fill_value_via_encoding_kwarg(self):
708+
ds = Dataset({'x': ('y', [np.pi, -np.pi])})
709+
kwargs = dict(encoding={'x': {'_FillValue': None}})
710+
with self.roundtrip(ds, save_kwargs=kwargs) as actual:
711+
assert '_FillValue' not in actual.x.encoding
712+
self.assertEqual(ds.y.encoding, {})
713+
714+
def test_explicitly_omit_fill_value_in_coord(self):
715+
ds = Dataset({'x': ('y', [np.pi, -np.pi])}, coords={'y': [0.0, 1.0]})
716+
ds.y.encoding['_FillValue'] = None
717+
with self.roundtrip(ds) as actual:
718+
assert '_FillValue' not in actual.y.encoding
719+
720+
def test_explicitly_omit_fill_value_in_coord_via_encoding_kwarg(self):
721+
ds = Dataset({'x': ('y', [np.pi, -np.pi])}, coords={'y': [0.0, 1.0]})
722+
kwargs = dict(encoding={'y': {'_FillValue': None}})
723+
with self.roundtrip(ds, save_kwargs=kwargs) as actual:
724+
assert '_FillValue' not in actual.y.encoding
725+
self.assertEqual(ds.y.encoding, {})
726+
707727
def test_encoding_same_dtype(self):
708728
ds = Dataset({'x': ('y', np.arange(10.0, dtype='f4'))})
709729
kwargs = dict(encoding={'x': {'dtype': 'f4'}})

0 commit comments

Comments
 (0)