Skip to content

Commit 44011c9

Browse files
Karel-van-de-Plasschedcherian
authored andcommitted
Fixes #2198: Drop chunksizes when only when original_shape is different, not when it isn't found (#2207)
* Fixes #2198: Drop chunksizes when original_shape is different Before this fix chunksizes was dropped even when original_shape was not found in encoding * More direct has_original_shape check * Fixed typo * Added test if chunksizes is kept when no original shape * Fix typo in test name Co-Authored-By: Deepak Cherian <[email protected]> * Fix keep_chunksizes_if_no_orignal_shape test by using native open_dataset * Added entry in whats-new * Use roundtrip mechanism in chunksizes conservation test
1 parent 5343ccc commit 44011c9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ v0.12.2 (unreleased)
2121
Enhancements
2222
~~~~~~~~~~~~
2323

24+
25+
- netCDF chunksizes are now only dropped when original_shape is different,
26+
not when it isn't found. (:issue:`2207`)
27+
By `Karel van de Plassche <https://github.com/Karel-van-de-Plassche>`_.
2428
- Enable `@` operator for DataArray. This is equivalent to :py:meth:`DataArray.dot`
2529
By `Maximilian Roos <https://github.com/max-sixty>`_.
2630
- Add ``fill_value`` argument for reindex, align, and merge operations

xarray/backends/netCDF4_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False,
206206
chunks_too_big = any(
207207
c > d and dim not in unlimited_dims
208208
for c, d, dim in zip(chunksizes, variable.shape, variable.dims))
209-
changed_shape = encoding.get('original_shape') != variable.shape
209+
has_original_shape = 'original_shape' in encoding
210+
changed_shape = (has_original_shape and
211+
encoding.get('original_shape') != variable.shape)
210212
if chunks_too_big or changed_shape:
211213
del encoding['chunksizes']
212214

xarray/tests/test_backends.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,18 @@ def test_encoding_kwarg_compression(self):
11341134

11351135
assert ds.x.encoding == {}
11361136

1137+
def test_keep_chunksizes_if_no_original_shape(self):
1138+
ds = Dataset({'x': [1, 2, 3]})
1139+
chunksizes = (2, )
1140+
ds.variables['x'].encoding = {
1141+
'chunksizes': chunksizes
1142+
}
1143+
1144+
with self.roundtrip(ds) as actual:
1145+
assert_identical(ds, actual)
1146+
assert_array_equal(ds['x'].encoding['chunksizes'],
1147+
actual['x'].encoding['chunksizes'])
1148+
11371149
def test_encoding_chunksizes_unlimited(self):
11381150
# regression test for GH1225
11391151
ds = Dataset({'x': [1, 2, 3], 'y': ('x', [2, 3, 4])})

0 commit comments

Comments
 (0)