Skip to content

Commit 1048df2

Browse files
allow other and drop arguments in where (gh#6466) (#6467)
* allow other and drop arguments in where (gh#6466) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add whatsnew entry to bug fixes? * drop extraneous edit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9b1e1b0 commit 1048df2

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Bug fixes
7878
By `Stan West <https://github.com/stanwest>`_.
7979
- Fix bug in :py:func:`where` when passing non-xarray objects with ``keep_attrs=True``. (:issue:`6444`, :pull:`6461`)
8080
By `Sam Levang <https://github.com/slevang>`_.
81+
- Allow passing both ``other`` and ``drop=True`` arguments to ``xr.DataArray.where``
82+
and ``xr.Dataset.where`` (:pull:`6466`, :pull:`6467`).
83+
By `Michael Delgado <https://github.com/delgadom>`_.
8184

8285
Documentation
8386
~~~~~~~~~~~~~

xarray/core/common.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,7 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):
11971197
By default, these locations filled with NA.
11981198
drop : bool, optional
11991199
If True, coordinate labels that only correspond to False values of
1200-
the condition are dropped from the result. Mutually exclusive with
1201-
``other``.
1200+
the condition are dropped from the result.
12021201
12031202
Returns
12041203
-------
@@ -1251,6 +1250,14 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):
12511250
[15., nan, nan, nan]])
12521251
Dimensions without coordinates: x, y
12531252
1253+
>>> a.where(a.x + a.y < 4, -1, drop=True)
1254+
<xarray.DataArray (x: 4, y: 4)>
1255+
array([[ 0, 1, 2, 3],
1256+
[ 5, 6, 7, -1],
1257+
[10, 11, -1, -1],
1258+
[15, -1, -1, -1]])
1259+
Dimensions without coordinates: x, y
1260+
12541261
See Also
12551262
--------
12561263
numpy.where : corresponding numpy function
@@ -1264,9 +1271,6 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):
12641271
cond = cond(self)
12651272

12661273
if drop:
1267-
if other is not dtypes.NA:
1268-
raise ValueError("cannot set `other` if drop=True")
1269-
12701274
if not isinstance(cond, (Dataset, DataArray)):
12711275
raise TypeError(
12721276
f"cond argument is {cond!r} but must be a {Dataset!r} or {DataArray!r}"

xarray/tests/test_dataset.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4548,8 +4548,11 @@ def test_where_other(self):
45484548
actual = ds.where(lambda x: x > 1, -1)
45494549
assert_equal(expected, actual)
45504550

4551-
with pytest.raises(ValueError, match=r"cannot set"):
4552-
ds.where(ds > 1, other=0, drop=True)
4551+
actual = ds.where(ds > 1, other=-1, drop=True)
4552+
expected_nodrop = ds.where(ds > 1, -1)
4553+
_, expected = xr.align(actual, expected_nodrop, join="left")
4554+
assert_equal(actual, expected)
4555+
assert actual.a.dtype == int
45534556

45544557
with pytest.raises(ValueError, match=r"cannot align .* are not equal"):
45554558
ds.where(ds > 1, ds.isel(x=slice(3)))

0 commit comments

Comments
 (0)