Skip to content

Commit 6b8c1b0

Browse files
0x0L0x0L
0x0L
authored and
0x0L
committed
dataset fix and more tests
1 parent b990042 commit 6b8c1b0

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

xarray/core/dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,11 +3278,11 @@ def rank(self, dim, pct=False, keep_attrs=False):
32783278

32793279
variables = OrderedDict()
32803280
for name, var in iteritems(self.variables):
3281-
if name in self.data_vars and dim in var.dims:
3282-
variables[name] = var.rank(dim, pct=pct)
3283-
variables.update({
3284-
k: self.variables[k] for k in var.dims
3285-
if k not in variables and k in self.variables})
3281+
if name in self.data_vars:
3282+
if dim in var.dims:
3283+
variables[name] = var.rank(dim, pct=pct)
3284+
else:
3285+
variables[name] = var
32863286

32873287
coord_names = set(k for k in self.coords if k in variables)
32883288
attrs = self.attrs if keep_attrs else None

xarray/tests/test_dataset.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3417,9 +3417,19 @@ def test_quantile(self):
34173417
@requires_bottleneck
34183418
def test_rank(self):
34193419
ds = create_test_data(seed=1234)
3420-
x = ds.rank('dim3').var3
3420+
# only ds.var3 depends on dim3
3421+
z = ds.rank('dim3')
3422+
self.assertItemsEqual(['var3'], list(z.data_vars))
3423+
# same as dataarray version
3424+
x = z.var3
34213425
y = ds.var3.rank('dim3')
34223426
self.assertDataArrayEqual(x, y)
3427+
# coordinates stick
3428+
self.assertItemsEqual(list(z.coords), list(ds.coords))
3429+
self.assertItemsEqual(list(x.coords), list(y.coords))
3430+
# invalid dim
3431+
with raises_regex(ValueError, 'does not contain'):
3432+
x.rank('invalid_dim')
34233433

34243434
def test_count(self):
34253435
ds = Dataset({'x': ('a', [np.nan, 1]), 'y': 0, 'z': np.nan})

xarray/tests/test_variable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,13 @@ def test_rank(self):
13761376
v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0])
13771377
v_expect = Variable(['x'], [0.75, 0.25, np.nan, 0.5, 1.0])
13781378
self.assertVariableEqual(v.rank('x', pct=True), v_expect)
1379+
# no dask arrays
1380+
w = v.chunk(2)
1381+
with raises_regex(TypeError, 'arrays stored as dask'):
1382+
w.rank('x')
1383+
# invalid dim
1384+
with raises_regex(ValueError, 'not found'):
1385+
v.rank('y')
13791386

13801387
def test_big_endian_reduce(self):
13811388
# regression test for GH489

0 commit comments

Comments
 (0)