Skip to content

Commit 1e275d3

Browse files
committed
Make sure that we don't rechunk the entire variable to one chunk
by reducing over all dimensions. Dask raises an error when axis=None but not when axis=range(a.ndim).
1 parent 54bea40 commit 1e275d3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

xarray/core/nanops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ def nanmean(a, axis=None, dtype=None, out=None):
143143

144144

145145
def nanmedian(a, axis=None, out=None):
146+
# The dask algorithm works by rechunking to one chunk along axis
147+
# Make sure we trigger the dask error when passing all dimensions
148+
# so that we don't rechunk the entire array to one chunk and
149+
# possibly blow memory
150+
if axis is not None and len(axis) == a.ndim:
151+
axis = None
146152
return _dask_or_eager_func(
147153
"nanmedian", dask_module=dask_array_compat, eager_module=nputils
148154
)(a, axis=axis)

xarray/tests/test_variable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,12 @@ def test_reduce_funcs(self):
16041604

16051605
assert_identical(v.max(), Variable([], pd.Timestamp("2000-01-03")))
16061606

1607+
@requires_dask
1608+
def test_median(self):
1609+
v = Variable(["x", "y"], self.d).chunk(2)
1610+
with raises_regex(NotImplementedError, "difficult to do in parallel"):
1611+
v.median(["x", "y"])
1612+
16071613
def test_reduce_keepdims(self):
16081614
v = Variable(["x", "y"], self.d)
16091615

0 commit comments

Comments
 (0)