diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1b2fe3fbf6b..5f3e855f8b1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -209,6 +209,10 @@ Bug fixes when objects other than ``Dataset`` are provided (:issue:`1555`). By `Joe Hamman `_. +- :py:func:`xarray.Dataset.copy` would not preserve the encoding property + (:issue:`1586`). + By `Guido Imperiale `_. + - :py:func:`xarray.concat` would eagerly load dask variables into memory if the first argument was a numpy variable (:issue:`1588`). By `Guido Imperiale `_. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 5d975ffd281..8eaecdde548 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -634,7 +634,8 @@ def copy(self, deep=False): for k, v in iteritems(self._variables)) # skip __init__ to avoid costly validation return self._construct_direct(variables, self._coord_names.copy(), - self._dims.copy(), self._attrs_copy()) + self._dims.copy(), self._attrs_copy(), + encoding=self.encoding) def _subset_with_all_valid_coords(self, variables, coord_names, attrs): needed_dims = set() diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index eda3b03a2e5..b0cad11e74e 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -50,6 +50,7 @@ def create_test_data(seed=None): obj[v] = (dims, data, {'foo': 'variable'}) obj.coords['numbers'] = ('dim3', np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], dtype='int64')) + obj.encoding = {'foo': 'bar'} assert all(obj.data.flags.writeable for obj in obj.values()) return obj @@ -1434,6 +1435,7 @@ def test_copy(self): for copied in [data.copy(deep=False), copy(data)]: self.assertDatasetIdentical(data, copied) + self.assertEqual(data.encoding, copied.encoding) # Note: IndexVariable objects with string dtype are always # copied because of xarray.core.util.safe_cast_to_index. # Limiting the test to data variables.