Skip to content

Commit

Permalink
add Variable._replace (#3528)
Browse files Browse the repository at this point in the history
* add Variable._replace

* assertions

* whatsew

* whatsnew
  • Loading branch information
max-sixty authored Nov 14, 2019
1 parent 4358762 commit 8b24037
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ Internal Changes
- Enable type checking on default sentinel values (:pull:`3472`)
By `Maximilian Roos <https://github.com/max-sixty>`_

- Add :py:meth:`Variable._replace` for simpler replacing of a subset of attributes (:pull:`3472`)
By `Maximilian Roos <https://github.com/max-sixty>`_

.. _whats-new.0.14.0:

v0.14.0 (14 Oct 2019)
Expand Down
19 changes: 17 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import functools
import itertools
import warnings
Expand All @@ -24,10 +25,11 @@
from .pycompat import dask_array_type, integer_types
from .utils import (
OrderedSet,
_default,
decode_numpy_dict_values,
either_dict_or_kwargs,
infix_dims,
ensure_us_time_resolution,
infix_dims,
)

try:
Expand Down Expand Up @@ -887,7 +889,20 @@ def copy(self, deep=True, data=None):
# note:
# dims is already an immutable tuple
# attributes and encoding will be copied when the new Array is created
return type(self)(self.dims, data, self._attrs, self._encoding, fastpath=True)
return self._replace(data=data)

def _replace(
self, dims=_default, data=_default, attrs=_default, encoding=_default
) -> "Variable":
if dims is _default:
dims = copy.copy(self._dims)
if data is _default:
data = copy.copy(self.data)
if attrs is _default:
attrs = copy.copy(self._attrs)
if encoding is _default:
encoding = copy.copy(self._encoding)
return type(self)(dims, data, attrs, encoding, fastpath=True)

def __copy__(self):
return self.copy(deep=False)
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ def test_copy_index_with_data_errors(self):
with raises_regex(ValueError, "must match shape of object"):
orig.copy(data=new_data)

def test_replace(self):
var = Variable(("x", "y"), [[1.5, 2.0], [3.1, 4.3]], {"foo": "bar"})
result = var._replace()
assert_identical(result, var)

new_data = np.arange(4).reshape(2, 2)
result = var._replace(data=new_data)
assert_array_equal(result.data, new_data)

def test_real_and_imag(self):
v = self.cls("x", np.arange(3) - 1j * np.arange(3), {"foo": "bar"})
expected_re = self.cls("x", np.arange(3), {"foo": "bar"})
Expand Down

0 comments on commit 8b24037

Please sign in to comment.