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

keep attrs in xarray.where #4687

Merged
merged 21 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ v0.17.0 (unreleased)

Breaking changes
~~~~~~~~~~~~~~~~

- xarray no longer supports python 3.6

The minimum versions of some other dependencies were changed:
Expand Down Expand Up @@ -51,9 +52,11 @@ Deprecations
version 0.19.0 (:pull:`3993`).
By `Tom Nicholas <https://github.com/TomNicholas>`_.


New Features
~~~~~~~~~~~~

- ``keep_attrs`` support for :py:func:`where` (:issue:`4141`, :issue:`4682`, :pull:`4687`).
By `Justus Magin <https://github.com/keewis>`_.
- Xarray now leverages updates as of cftime version 1.4.1, which enable exact I/O
roundtripping of ``cftime.datetime`` objects (:pull:`4758`).
By `Spencer Clark <https://github.com/spencerkclark>`_.
Expand Down
5 changes: 5 additions & 0 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,10 @@ def where(cond, x, y):
In priority order: Dataset, DataArray, Variable or array, whichever
type appears as an input argument.

Notes
-----
Only the attrs on `cond` will be kept.

Examples
--------
>>> import xarray as xr
Expand Down Expand Up @@ -1578,6 +1582,7 @@ def where(cond, x, y):
join="exact",
dataset_join="exact",
dask="allowed",
keep_attrs=True,
)


Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,13 @@ def test_where():
assert_identical(expected, actual)


def test_where_attrs():
cond = xr.DataArray([True, False], dims="x", attrs={"attr": "value"})
actual = xr.where(cond, 1, 0)
expected = xr.DataArray([1, 0], dims="x", attrs={"attr": "value"})
assert_identical(expected, actual)


@pytest.mark.parametrize("use_dask", [True, False])
@pytest.mark.parametrize("use_datetime", [True, False])
def test_polyval(use_dask, use_datetime):
Expand Down
5 changes: 1 addition & 4 deletions xarray/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,10 +2417,7 @@ def test_binary_operations(self, func, dtype):
(
pytest.param(operator.lt, id="less_than"),
pytest.param(operator.ge, id="greater_equal"),
pytest.param(
operator.eq,
id="equal",
),
pytest.param(operator.eq, id="equal"),
),
)
@pytest.mark.parametrize(
Expand Down