Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Variable._replace #3528

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ Internal Changes
- Enable type checking on default sentinel values (:pull:`3472`)
By `Maximilian Roos <https://github.com/max-sixty>`_

- Add :py:meth:`DataArray._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