Skip to content

Commit

Permalink
Dataset.copy() to preserve encoding (#1590)
Browse files Browse the repository at this point in the history
* Dataset.copy() to preserve encoding

* Fix sphinx hook

* Add unit test
  • Loading branch information
crusaderky authored and shoyer committed Oct 8, 2017
1 parent 5bd4015 commit 772f7e0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ Bug fixes
when objects other than ``Dataset`` are provided (:issue:`1555`).
By `Joe Hamman <https://github.com/jhamman>`_.

- :py:func:`xarray.Dataset.copy` would not preserve the encoding property
(:issue:`1586`).
By `Guido Imperiale <https://github.com/crusaderky>`_.

- :py:func:`xarray.concat` would eagerly load dask variables into memory if
the first argument was a numpy variable (:issue:`1588`).
By `Guido Imperiale <https://github.com/crusaderky>`_.
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 772f7e0

Please sign in to comment.